4

我已经用 c++ 编写了一些代码,但是当我尝试编译时,我得到了一个非常奇怪的错误,我不应该有。这是我的代码:

#include <iostream>
using namespace std;
#include "articles.h"
int main() {
  // Création des articles
  const Stylo s1 ("s1", "Stylo jade", "Watertruc", 500, "Noir");
  const Ramette r1 ("r1", "Ramette haute qualité", "Clairefont", 95, 80);
  // Création des lots (10 % de réduction)
  const Lot l1 ("l1", s1, 5, 10);
  cout << s1 << "\n" << r1 << "\n" << l1 << "\n";
}

文章.h

#ifndef ARTICLES_H
#define ARTICLES_H
using namespace std;
#include <string>
#include <iostream>





class Article
{
    public:
    string getReference() const ;
    virtual string getDescriptif() const;
    virtual double getPU() const;
    string getMarque() const;
    virtual void Afficher(ostream&) const;
    ~Article();

    protected:
    Article(string);
        string reference;
    private:

};
//ostream& operator<<(ostream& os, const Article& art);

class ArticleUnitaire : public Article {
    public:
      ArticleUnitaire(string);
      ArticleUnitaire(string, string, string, double);
      string getMarque() const;
      void setMarque(string&);
      double getPU() const;
      void setPU(double&);
      void setDescriptif(string&);
      string getDescriptif() const;
      virtual void Afficher(ostream&) const;
      ~ArticleUnitaire();

    protected:
      string marque;
      double pu;
      string descriptif;
    private:

};
inline ostream& operator<<(ostream& os, const Article& art) {
  art.Afficher(os);
  return os;
}



class Stylo : public ArticleUnitaire {
    public:
      Stylo(string, string, string, double, string);
      virtual void Afficher(ostream&) const;
      string getCouleur() const;
      ~Stylo();
    protected:
    private:
      string couleur;

};

class Ramette : public ArticleUnitaire {
    public:
      Ramette(string, string, string, double, int);
      virtual void Afficher(ostream&) const;
      int getGrammage() const;
      ~Ramette();
    protected:
    private:
      int grammage;
};

class Lot : public Article {
    public:
      Lot(string, Article, int, int);
      double getPU() const;
      string getDescriptif() const;
      string getMarque() const;
      int getNbArticles() const;
      void setNbArticles(int&);
      int getPourcentage() const;
      void setPourcentage(int&);
      Article getArticle() const;
      void setArticle(Article&);
      virtual void Afficher(ostream&) const;
      ~Lot();


    protected:
    private:
      int nb;
      int pourcentage;
      Article art;
};


#endif // ARTICLES_H

文章.cc

using namespace std;
#include <typeinfo>
#include <string>
#include <sstream>
#include "articles.h"

/*   Article   */

Article::Article(string ref) : reference(ref) {};

string Article::getReference() const {
  return reference;
}


string Article::getMarque() const {
  return "Sans marque";
}

void Article::Afficher(ostream& os) const {
  os << " : reference = " << getReference() << " ; descriptif = " << getDescriptif() << " ; marque = " << getMarque() << " ; PU = " << getPU();
}

Article::~Article() {};


/*   Article Unitaire   */

ArticleUnitaire::ArticleUnitaire(string r) : Article(r) {};

ArticleUnitaire::ArticleUnitaire(string r, string d, string m, double p) : Article(r), marque(m), descriptif(d), pu(p) {};

string ArticleUnitaire::getMarque() const {
  return marque;
}

void ArticleUnitaire::setMarque(string& m) {
  marque = m;
}

double ArticleUnitaire::getPU() const {
  return pu;
}

void ArticleUnitaire::setPU(double& p) {
  pu = p;
}

void ArticleUnitaire::setDescriptif(string& d) {
  descriptif = d;
}

string ArticleUnitaire::getDescriptif() const {
  return descriptif;
}

ArticleUnitaire::~ArticleUnitaire() {};

/*   Stylo    */

Stylo::Stylo(string r, string d, string m, double p, string c) : ArticleUnitaire(r,d,m,p), couleur(c) {};

string Stylo::getCouleur() const {
  return couleur;
}
void Stylo::Afficher(ostream& os) const {
  Article::Afficher(os);Lot
  os << " ; couleur = " << getCouleur();
}

Stylo::~Stylo() {};

/*   Ramette   */  
Ramette::Ramette(string r, string d, string m, double p, int g) : ArticleUnitaire(r,d,m,p), grammage(g) {};

int Ramette::getGrammage() const {
  return grammage;
}

void Ramette::Afficher(ostream& os) const {
  Article::Afficher(os);
  os << " ; grammage = " << getGrammage();
}

Ramette::~Ramette() {};

/*   Lot   */

Lot::Lot(string r, Article a, int n, int p) : Article(r), art(a), nb(n), pourcentage(p) {};

double Lot::getPU() const {
  return nb * art.getPU() * (100 - pourcentage) / 100;
}

string Lot::getDescriptif() const {
  stringstream sstm;
  sstm << "Lot de" << nb << " *" << art.getDescriptif() << "* ";
  return sstm.str();
}

string Lot::getMarque() const {
  return art.getMarque();
}

int Lot::getNbArticles() const {
  return nb;
}

void Lot::setNbArticles(int& nbArticles) {
  nb = nbArticles;
}

int Lot::getPourcentage() const {
  return pourcentage;
}

void Lot::setPourcentage(int& p) {
  pourcentage = p;
}

Article Lot::getArticle() const {
  return art;
}

void Lot::setArticle(Article& a) {
  art = a;
}

void Lot::Afficher(ostream& os) const {
  Article::Afficher(os);
  os << " ;reduction = " << getPourcentage() << " ; compose de " << getNbArticles() << " [" << art << " ]";
}

Lot::~Lot() {};

当我编译时:

g++ -Wall testArticles.cc
/tmp/ccwWjTWv.o: In function `main':
testArticles.cc:(.text+0xe8): undefined reference to `Stylo::Stylo(std::string, std::string, std::string, double, std::string)'
testArticles.cc:(.text+0x218): undefined reference to `Ramette::Ramette(std::string, std::string, std::string, double, int)'
testArticles.cc:(.text+0x2da): undefined reference to `Lot::Lot(std::string, Article, int, int)'
testArticles.cc:(.text+0x307): undefined reference to `Article::~Article()'
testArticles.cc:(.text+0x36f): undefined reference to `Lot::~Lot()'
testArticles.cc:(.text+0x37b): undefined reference to `Ramette::~Ramette()'
testArticles.cc:(.text+0x387): undefined reference to `Stylo::~Stylo()'
testArticles.cc:(.text+0x3b4): undefined reference to `Stylo::~Stylo()'
testArticles.cc:(.text+0x3e8): undefined reference to `Stylo::~Stylo()'
testArticles.cc:(.text+0x41c): undefined reference to `Stylo::~Stylo()'
testArticles.cc:(.text+0x450): undefined reference to `Stylo::~Stylo()'
testArticles.cc:(.text+0x48c): undefined reference to `Ramette::~Ramette()'
testArticles.cc:(.text+0x4c0): undefined reference to `Ramette::~Ramette()'
testArticles.cc:(.text+0x4f4): undefined reference to `Ramette::~Ramette()'
testArticles.cc:(.text+0x533): undefined reference to `Lot::~Lot()'
testArticles.cc:(.text+0x556): undefined reference to `Article::~Article()'
testArticles.cc:(.text+0x56a): undefined reference to `Lot::~Lot()'
testArticles.cc:(.text+0x57e): undefined reference to `Lot::~Lot()'
testArticles.cc:(.text+0x58f): undefined reference to `Ramette::~Ramette()'
testArticles.cc:(.text+0x5a0): undefined reference to `Stylo::~Stylo()'
/tmp/ccwWjTWv.o: In function `Article::Article(Article const&)':
testArticles.cc:(.text._ZN7ArticleC2ERKS_[_ZN7ArticleC5ERKS_]+0x17): undefined reference to `vtable for Article'
collect2: error: ld returned 1 exit status

我在每个可能的网站上上网,每个人都说同样的话:看起来我搞砸了#include(s)。但我一直非常小心,我确信这是关于一些愚蠢和次要的事情,但已经 6 个小时了,我开始放弃了。这很奇怪。

4

3 回答 3

5

您需要确保将所有 .cc 文件编译在一起。这样做g++ -Wall articles.cc testArticles.cc,或者你的文件实际上被调用了什么。这会将每个文件编译成一个 .o(对象)文件并为您将它们链接在一起。如果需要,您可以分多个步骤执行此操作:

g++ -Wall -c articles.cc
g++ -Wall -c testArticles.cc
g++ articles.o testArticles.o
于 2012-12-21T00:57:56.363 回答
3

您需要在命令行参数中包含所有源文件:

g++ -Wall testArticles.cc articles.cc

或者,您可以创建一个 makefile 来定义源文件之间的依赖关系。对于 C++ 程序员来说,这是一个方便的工具,尤其是在 *nix 环境中。

于 2012-12-21T00:57:03.710 回答
2

您正在编译和链接testArticles.cc,但您没有编译articles.cc。这就是您收到链接器错误的原因;您实际上并没有在编译器可以看到的任何地方实现这些方法。

尝试使用

g++ -Wall articles.cc testArticles.cc
于 2012-12-21T00:57:13.307 回答