我用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";
}
字符串
articles.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
型
articles.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搞砸了。但我一直很小心,我确信这是关于一些愚蠢和次要的事情,但已经6个小时了,我开始给予了。这很奇怪。
4条答案
按热度按时间erhoui1w1#
您需要确保将所有.cc文件编译在一起。因此,执行
g++ -Wall articles.cc testArticles.cc
或任何实际调用的文件。这将将每个文件编译为.o(对象)文件并将它们链接在一起。如果需要,您可以分多个步骤执行此操作:字符串
r9f1avp52#
您需要在命令行参数中包含所有源文件:
字符串
或者,你可以创建一个makefile来定义你的源文件之间的依赖关系。这是C++程序员的一个方便的工具,特别是在 *nix环境中。
yks3o0rb3#
你正在编译和链接
testArticles.cc
,但你没有编译articles.cc
。这就是为什么你会得到链接器错误;你实际上没有在编译器可以看到的任何地方实现方法。尝试使用
字符串
ruoxqz4g4#
先前的答案给了我线索。
我的visual studio代码任务如下所示,导致了错误。
字符串
我用
${workspaceFolder}/*.cpp
替换了${file}
参数。看看这个引用。现在它编译得很好。