gpt4 book ai didi

c++ - 未定义对析构函数的引用

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:15:05 26 4
gpt4 key购买 nike

我已经用 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 个小时了,我开始放弃了。这很奇怪。

最佳答案

您需要确保将所有 .cc 文件编译在一起。 g++ -Wall articles.cc testArticles.cc 或任何实际调用的文件也是如此。这会将每个文件编译成一个 .o(目标)文件并为您将它们链接在一起。如果需要,您可以分多个步骤执行此操作:

g++ -Wall -c articles.cc
g++ -Wall -c testArticles.cc
g++ articles.o testArticles.o

关于c++ - 未定义对析构函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com