60

I am getting an undefined reference to `vtable for student' while compiling the following header file:

student.h

class student
{
private:
    string names;
    string address;
    string type;

protected:
    float marks;
    int credits;

public:
    student();
    student(string n,string a,string t,float m);
    ~student();
    string getNames();
    string getAddress();
    string getType();
    float getMarks();
    virtual void calculateCredits();
    int getCredits();
};

student::student(){}

student::student(string n, string a,string t,float m)
{
    names = n;
    address = a;
    marks = m;
}

student::~student(){}

I can't find what is wrong in this.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Tarounen
  • 1,119
  • 3
  • 14
  • 25

1 Answers1

107

You're declaring a virtual function and not defining it:

virtual void calculateCredits();

Either define it or declare it as:

virtual void calculateCredits() = 0;

Or simply:

virtual void calculateCredits() { };

Read more about vftable: http://en.wikipedia.org/wiki/Virtual_method_table

  • thank you, it worked.. actually this is only part of the header file, i have another class below which uses the function calculateCredits() i thought there was no need to define in the student class.. – Tarounen Apr 23 '14 at 21:21
  • 4
    In most ABIs, the vtable is emitted in the compilation unit defining the first virtual function not defined in the class definition. If there is none such, it will be emitted in all. Multiple such objects will then be folded. – Deduplicator Apr 23 '14 at 21:22
  • 9
    I'm getting the same error despite defining the virtual function as shown above. Could there be anything else I am missing? – Naveen Sep 25 '15 at 21:08