0

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.

6
  • 1
    Are you sure you have a definition of virtual ~polynomial() ? (and not just a declaration). Commented Nov 20, 2014 at 13:13
  • an executing example for problem would help in answering the question. Commented Nov 20, 2014 at 13:14
  • have you implemented inverse in child classes? Commented Nov 20, 2014 at 13:14
  • 1
    It sounds like you didn't define all the virtual functions declared in affine (as the note says in the error message). Specifically, did you define the first one, affine::clone? Commented Nov 20, 2014 at 13:17
  • I've implemented ~polynomial() like this : polynomial::~polynomial() { delete[] coefficient; } In what concerns 'inverse', I just implemented it for 'polynomial'. There's no virtual methods in 'affine', but I did not defined all the methods in 'affine', because I would like to test 'polynomial' and its methods first before going on something else Commented Nov 20, 2014 at 13:29

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Should I implement all 'affine' methods, even if I'm not using them all in the main file (just testing some methods I've written so far)?
@dada: You must define every non-pure virtual function you declare. Since they all override functions defined in polynomial, you could remove/comment out the declarations until you need them; or you could write a minimal definition, e.g. returning a dummy value, or calling the base-class version.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.