【问题标题】:Undefined symbols for architecture x86_64 (C++ Inheritence Issues)架构 x86_64 的未定义符号(C++ 继承问题)
【发布时间】:2014-12-06 04:43:16
【问题描述】:

这是我在编译时遇到的错误:

Undefined symbols for architecture x86_64:
  "typeinfo for BaseClass", referenced from:
      typeinfo for DerivedOne in base-49c1cd.o
      typeinfo for DerivedTwo in base-49c1cd.o
  "vtable for BaseClass", referenced from:
      BaseClass::BaseClass() in base-49c1cd.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

这里是base.h

class BaseClass {
public:
    // an enum
protected:
    float variable
public:
    float getVariable();
    void printBase();
    virtual BaseClass* clone();
    virtual float calculate(bool _one, bool _two) = 0;
};

class DerivedOne: public BaseClass {
public:
    DerivedOne(float _variable);
    BaseClass* clone();
    float calculate(bool _one, bool _two);
};

class DerivedTwo: public BaseClass {
public:
    DerivedTwo(float _variable);
    BaseClass* clone();
    float calculate(bool _one, bool _two);
};

base.cpp:

#include "base.h"

float BaseClass::getVariable() {
    return variable;
}

void BaseClass::printBase() {
    return; // not implemented yet
}

DerivedOne::DerivedOne(float _variable) {
    variable = _variable;
}

BaseClass* DerivedOne::clone() {
    DerivedOne* tmp = new DerivedOne(variable);
    return tmp;
}

float DerivedOne::calculate(bool _one, bool _one) {
    float val = //some calculation;
    return val;
}

DerivedTwo::DerivedTwo(float _variable) {
    variable = _variable;
}

BaseClass* DerivedTwo::clone() {
    DerivedTwo* tmp = new DerivedTwo(variable);
    return tmp;
}

float DerivedTwo::calculate(bool _one, bool _two) {
    float val = //some calculation;
    return val;
}

我已经更改了变量的名称,所以我可能打错了。

我认为我遇到的问题源于我对构造函数和抽象类缺乏了解。谁能帮我解决问题?

【问题讨论】:

    标签: c++ class inheritance abstract-class virtual-functions


    【解决方案1】:

    您没有提供BaseClass:clone 方法的实现。要么让它成为纯虚拟的,即=0,要么提供一个实现。

    错误信息基本上说明了整个故事:

    注意:缺少 vtable 通常意味着第一个非内联虚拟成员函数没有定义。

    您提供了声明,但没有提供定义。

    【讨论】:

    • 谢谢,我想我并没有真正理解虚拟和严格虚拟之间的区别(我知道这是有名字的)。我认为我不需要在 BaseClass 中声明 clone() ,因为它是在派生类中实现的。谢谢!
    • 另外,它是“纯虚拟的”,而不是抽象的。当一个类(无论是否继承)至少有一个纯虚函数时,它就是抽象的。你真的知道除了使用=0; 来声明一个纯虚拟成员之外的其他方法吗?
    【解决方案2】:

    请尝试在“base.cpp”中添加以下内容

    BaseClass::BaseClass()
    {
        //do something (if there's any)
    }
    BaseClass::~BaseClass()
    {
        //do something (if there's any)
    }
    BaseClass::clone()
    {
        //do something (if there's any)
    }
    

    :-)

    链接器正在寻找您的 BaseClass 的定义。 如果您不使用上面示例中的代码编写它,它将产生未定义的引用链接器错误。 而clone(),由于它不是纯虚的,所以需要为它创建一个默认的实现,这样当派生类没有为它创建实现时,就会使用基类中的实现。

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      相关资源
      最近更新 更多