链接错误缺少vtable

时间:2014-11-20 13:09:35

标签: c++ xcode4 vtable

我正在定义一个类'function'和另外两个继承'function'的类'polynomial'和'affine'。

    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)

我找不到原因。

2 个答案:

答案 0 :(得分:0)

阅读错误讯息:

  

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

您似乎没有实现第一个非内联虚拟成员函数。查看类定义,即afine::clone

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

答案 1 :(得分:0)

正如你们所说,这是关于“仿射”的未定义的重写方法。