链接错误缺少 vtable

Link error missing vtable

本文关键字:vtable 错误 链接      更新时间:2023-10-16

我正在定义一个类"函数"和另外两个从"函数"继承的类"多项式"和"仿射"。

    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 .

您描述仅实现构造函数。您还必须实现所有非纯虚函数。

正如你们所说,这是关于"仿射"的未定义的覆盖方法。