Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ undefined reference to vtable

Tags:

I'm learning C++. I'm trying to do an exercise where I define several implementations of a pure virtual class with a single function. I'm having trouble linking the class that uses these implementations.

==> BasicMath.h <==
#ifndef BASIC_MATH_H
#define BASIC_MATH_H

#include<string>
#include<vector>    

class BasicMath { };


#endif // BASIC_MATH_H

==> Operation.h <==

#ifndef OPERATION
#define OPERATION

#include<string>
#include<vector>    

class Operation {
 public:
  virtual void perform(std::vector<std::string> vec) = 0;
};


#endif // OPERATION

==> Sum.h <==
#ifndef SUM_H
#define SUM_H

#include "Operation.h"

class Sum: public Operation {
 public:
  void perform(std::vector<std::string> vec);
};

#endif // SUM_H

==> BasicMath.cpp <==
#ifndef BASIC_MATH_C
#define BASIC_MATH_C

#include <string>
#include <vector>
#include <iostream>
#include "BasicMath.h"
#include "Sum.h"

int main(int argc, char* argv[]) {
  Sum op;
}

#endif // BASIC_MATH_C

==> Sum.cpp <==
#ifndef SUM_C
#define SUM_C

#include <vector>
#include <string>
#include <iostream>
#include "Sum.h"

void Sum::perform(std::vector<std::string> vec) {
    using namespace std;
    int total = 0;
    cout << "Total: " << total << "\n";
};

#endif // SUM_C

Compilation:

$ g++ -c Sum.cpp
$ g++ -o BasicMath BasicMath.cpp
/tmp/cc1VXjNl.o:BasicMath.cpp:(.text$_ZN3SumC1Ev[Sum::Sum()]+0x16): undefined reference to `vtable for Sum'
collect2: ld returned 1 exit status

I'm 95% sure I'm doing at least one foolish thing here - but my brain is refusing to tell me what.

I have see this question but have not managed to fix my issue.

like image 303
Ben Fitzgerald Avatar asked Nov 24 '10 23:11

Ben Fitzgerald


People also ask

What does undefined reference to vtable mean?

In summary, there are three key causes of the "undefined reference to vtable" error: A member function is missing its definition. An object file is not being linked. All virtual functions have inline definitions.17-Jun-2010.

Do all C++ classes have a vtable?

Only classes with virtual member functions and/or a virtual destructor have a vtable.

Does every object have a vtable?

All classes with a virtual method will have a single vtable that is shared by all objects of the class. Each object instance will have a pointer to that vtable (that's how the vtable is found), typically called a vptr. The compiler implicitly generates code to initialize the vptr in the constructor.


2 Answers

I Just encountered the same problem, but my problem was that I had not written the destructor code in my .cpp file.

class.h:

class MyClass {
public:
    MyClass();
    virtual ~MyClass();
};

class.cpp:

MyClass::MyClass() {}

It just gave me the vtable error message, and implementing the (empty) destructor solved the problem.

[Edit] Thus, the corrected class file looks like this:

MyClass::MyClass() {}
MyClass::~MyClass() {}
like image 149
Till Kolditz Avatar answered Oct 08 '22 01:10

Till Kolditz


You're not including the Sum.o object file on your compile&link line (second g++ use).

like image 31
Edward Strange Avatar answered Oct 08 '22 02:10

Edward Strange