gpt4 book ai didi

c++ - 创建类对象后对 vtable 的 undefined reference

转载 作者:行者123 更新时间:2023-11-28 07:36:49 25 4
gpt4 key购买 nike

<分区>

我有以下两个类:

class Grill
{
public:
virtual ~Grill();
virtual std::string get_model() const{return _model;};
virtual void set_price(float p){_price=p;};
virtual float get_price() const {return _price;};
virtual Grill* clone()=0;
protected:
Grill(std::string m,float p):_model(m),_price(p){};
Grill(const Grill& obj)=default;
Grill(Grill&& obj)=default;
std::string _model;
float _price;
private:
Grill& operator=(const Grill& obj)=delete;
};


class Grill_Charcoal final : public Grill
{
public:
Grill_Charcoal(std::string m,float p):Grill(m,p){};
~Grill_Charcoal(){};
Grill_Charcoal* clone() override{return new Grill_Charcoal(*this);};
protected:
Grill_Charcoal(const Grill_Charcoal& obj)=default;
Grill_Charcoal(Grill_Charcoal&& obj)=default;
private:
Grill_Charcoal& operator=(const Grill_Charcoal& obj)=delete;
};

主要我想创建一个 vector存储指向 Grill 对象的指针。所以我创建了 vector<Grill*> .问题是当我创建一个对象时。我的主要是:

int main()
{
using namespace std;

vector<Grill*> grill;

Grill_Charcoal* gp=new Grill_Charcoal("adsaas",2312); //Error
//grill.push_back(new Grill_Charcoal("Sdsaa",22)); //Error as well, not compiled

return 0;
}

g++ 显示以下错误:

In function `Grill::Grill(std::string, float)':
program6.cc:(.text._ZN5GrillC2ESsf[_ZN5GrillC5ESsf]+0x1c): undefined reference to `vtable for Grill'
/tmp/ccjLugR9.o: In function `Grill_Charcoal::~Grill_Charcoal()':
program6.cc:(.text._ZN14Grill_CharcoalD2Ev[_ZN14Grill_CharcoalD5Ev]+0x1f): undefined reference to `Grill::~Grill()'
/tmp/ccjLugR9.o: In function `Grill::Grill(Grill const&)':
program6.cc:(.text._ZN5GrillC2ERKS_[_ZN5GrillC5ERKS_]+0x17): undefined reference to `vtable for Grill'
/tmp/ccjLugR9.o:(.rodata._ZTI14Grill_Charcoal[_ZTI14Grill_Charcoal]+0x10): undefined reference to `typeinfo for Grill'
collect2: error: ld returned 1 exit status

有什么解释为什么会这样吗?

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