0

I have a interface and I want to create a header with the functions from the interface and a .cpp implementing the functions in this header. But when try this I always get the problem undefined reference to 'vtable for Test' in the Testt.h file.

I'm working on a rather big project in eclipse so I reduced my problem to a few small classes.

ITestAdapter.h:

#ifndef ITESTADAPTER_H_
#define ITESTADAPTER_H_

class TestAdapter {
public:
virtual int test() = 0;
};

#endif /* ITESTADAPTER_H_ */

Testt.h:

#ifndef TESTT_H_
#define TESTT_H_
#include "ITestAdapter.h"

class Test: public TestAdapter{
public:
virtual int test();
};

#endif /* TESTT_H_ */

Testt.cpp:

#include "Testt.h"
int test() {
return 0;
}

Test_main.cpp:

#include <iostream>
#include "Testt.h"
using namespace std;

int main() {
Test t;
int i = t.test();
cout << i << endl;
return 0;
}

If I don't use the Testt.h at all and implement the interface within Testt.cpp and include Testt.cpp (what I want to avoid) within the file with my main-method then it works fine.

Testt.cpp (modified):

#include "ITestAdapter.h"
class Test: public TestAdapter {
public:
int test() {
    return 0;
}
};

So I don't understand why it doesn't work if I use a header (which I think would be the nicer solution).

I hope I could explain clearly what my problem is. If not please ask.

  • I have found this discussion, too. But somehow I just tried using destructors instead of the simple solution below. – user2018809 Aug 05 '13 at 10:50

2 Answers2

3

You are defining a non-member function int test() in Testt.cpp. You need to define int Test::test():

int Test::test()
{// ^^^^
  return 0;
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

Undefined reference to X means that the linker can't find the definition of X that has been declared.

You declared Test to have a member function int test() but this

int test() {
   return 0;
}

defines a free function.

You need

int Test::test() {
    return 0;
}

undefined reference to 'vtable for Test'" to test is a bit confusing. It usualy means that you forgot to define the first virtual function of your class.

jrok
  • 54,456
  • 9
  • 109
  • 141