【问题标题】:Undefined reference to vtable - C++ linking error未定义对 vtable 的引用 - C++ 链接错误
【发布时间】:2014-10-15 14:34:51
【问题描述】:

我很确定我在 WaterHeater 及其派生类中定义了每个函数,所以任何人都可以告诉我为什么会出现此链接错误?在此先谢谢大家,我已经有一段时间没有找到任何解决方案了。

这是我在编译控制台项目时遇到的错误:

heater.cpp:(.text$_ZN11WaterHeaterC2ERKS_[_ZN11WaterHeaterC2ERKS_]+0x13): undefined reference to `vtable for WaterHeater'
[Error] Id returned exit 1 status
recipe for target 'LabProject4.exe' failed

这是我的代码:

加热器.h

class WaterHeater{

    public:

        void print() const;
        virtual WaterHeater* clone() const;
        virtual float installedPrice( const bool _hookUp, const bool _newVent) const = 0;
        float getPrice() const{ return d_price; };
};

class Tankless: public WaterHeater{

    public:

        Tankless( ...){} const;
};

class StorageTank: public WaterHeater{

    public:

        StorageTank( ...){} const;
};


float Tankless::installedPrice( const bool _hookUp, const bool _newVent ) const{...}

WaterHeater* Tankless::clone() const{...}

float StorageTank::installedPrice( const bool _hookUp, const bool _newVent ) const{...}

WaterHeater* StorageTank::clone() const{...}

void WaterHeater::print() const{...}

【问题讨论】:

  • 请缩小范围。

标签: c++ compiler-errors linker virtual linker-errors


【解决方案1】:

如果您的 sn-ps 是用于编译程序的实际 sn-ps,则问题是 WaterHeater::clone 方法没有实现。 它是一个虚方法,因此输出了错误。

【讨论】:

    【解决方案2】:

    这是因为您没有为 WaterHeater 的两个虚拟对象定义实体。我知道它们会被派生类覆盖,但是你应该让它们纯(= 0;)或提供一些虚拟体。

    【讨论】:

    • 啊哈:我看到 installedPrice 是纯的,所以问题出在克隆上,正如另一个答案所述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多