Link error missing vtable

3.7k views Asked by At

I'm defining a class 'function' and two others classes 'polynomial' and 'affine' that inherit from 'function'.

    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(){}
    };

I implemented all the methods relating to 'polynomial' and would like to test them. For the derivative method, I'm using the affine constructor in the case the instance's degree is equal to 0. So I have to define this constructor before running tests.

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_;
}

My first test is :

#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;
}

When I run it, I get a link error :

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)

I can't find out why.

2

There are 2 answers

0
dada On

As you guys said, it was about the non defined overriding methods of 'affine'.

2
Mike Seymour On

Reading the error message:

NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

it seems likely that you haven't implemented the first non-inline virtual member function. Looking at the class definition, that is afine::clone.

You describe implementing only the constructor. You must also implement all non-pure virtual functions.