链接错误缺少 vtable
Link error missing vtable
我正在定义一个类"函数"和另外两个从"函数"继承的类"多项式"和"仿射"。
class function {
public:
function(){};
virtual function* clone()const=0;
virtual float operator()(float x)const=0; //gives the image of a number by the function
virtual function* derivative()const=0;
virtual float inverse(float y)const=0;
virtual ~function(){}
};
class polynomial : public function {
protected:
int degree;
private:
float *coefficient;
public:
polynomial(int d);
virtual~polynomial();
virtual function* clone()const;
int get_degree()const;
float operator[](int i)const; //reads coefficient number i
float& operator[](int i); //updates coefficient number i
virtual float operator()(float x)const;
virtual function* derivative()const;
virtual float inverse(float y)const;
};
class affine : public polynomial {
int a;
int b;
//ax+b
public:
affine(int d,float a_, float b_);
function* clone()const;
float operator()(float x)const;
function* derivative()const;
float inverse(float y)const;
~affine(){}
};
我实现了与"多项式"相关的所有方法,并想测试它们。对于导数方法,在实例的度数等于 0 的情况下,我使用仿射构造函数。所以我必须在运行测试之前定义这个构造函数。
polynomial::polynomial(int d)
{
assert(d>=0);
degree=d;
coefficient=new float [d+1];
}
polynomial::~polynomial()
{
delete[] coefficient;
}
function* polynomial::derivative()const
{
if(degree==0)
{
return new affine(0,0,0);
}
polynomial* deriv=new polynomial(degree-1);
for(int i=0;i<degree;i++)
deriv[i]=(i+1)*coefficient[i+1];
return deriv;
}
affine::affine(int d,float a_, float b_):polynomial(d)
{
assert(d==0 || d==1);
degree=d;
a=a_;
b=b_;
}
我的第一个测试是:
#include "function.h"
int main(int argc, const char * argv[])
{
//p=x^3
polynomial p(3);
for(int i=0;i<=2;i++) p[i]=0;
p[3]=1;
cout<<"21^(1/3)="<<p.inverse(21);
return 0;
}
当我运行它时,我收到一个链接错误:
Undefined symbols for architecture x86_64:
"vtable for affine", referenced from:
affine::affine(int, float, float) in polynomial.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)
我找不到为什么。
阅读错误消息:
注意:缺少 vtable 通常意味着第一个非内联虚拟成员函数没有定义。
您似乎尚未实现第一个非内联虚拟成员函数。查看类定义,即afine::clone
.
您描述仅实现构造函数。您还必须实现所有非纯虚函数。
正如你们所说,这是关于"仿射"的未定义的覆盖方法。
相关文章:
- 纯虚拟类和错误未定义对 'vtable 的引用
- 看起来如此主要的错误.cpp:(.text.startup+0xd6):未定义对"vtable for Counter"的引用?
- 使用 CMake 编译时,更改头文件的位置会导致缺少 vtable 错误
- 未定义对 vtable 错误的引用
- CMake 构建错误 - 虚拟方法错误缺少 vtable
- 多个vtable,在打电话时出现错误
- 访问VTable直接发出未定义的错误
- 实现纯虚拟方法后,VTable C 错误
- "vtable"链接器错误(涉及带有"=default"的虚拟析构函数) - Clang 3.1 中的潜在错误?
- 使用CMake构建Qt项目并从QMainWindow继承会导致未引用的vtable错误
- 如何解决"未定义对'vtable for <>的引用<ClassName>"错误?
- 奇怪的链接器错误未定义对 vtable 的引用
- 对受内联构造函数影响的vtable错误的未定义引用
- 是现代g++编译器解决的vtable错误的未定义引用
- 我将如何从这个抽象类创建一个子类?Vtable 错误
- 来自 vtable 的未定义符号是否意味着接口和实现之间存在错误
- 简单的应用程序"undefined reference to vtable"错误?[Qt]
- 对Vtable错误的奇怪的未定义引用
- EMF文件(.so)调试,符号找不到VTable错误
- 对"用于实现的 vtable"错误的未定义引用