0

我有以下两个课程:

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

任何解释为什么会发生这种情况?

4

1 回答 1

1

错误消息为您说明了......您声明了 ~Grill() 但您从未定义它。

于 2013-05-19T06:39:18.797 回答