undefined reference to `vtable for KL'

Please explain this compile error:
D:\debug>g++ tester.cpp
C:\Users\wolf\AppData\Local\Temp\ccXUxmzL.o:tester.cpp:(.text$_ZN2KLC1Ev[__ZN2KL
C1Ev]+0x18): undefined reference to `vtable for KL'
collect2.exe: error: ld returned 1 exit status


I read about vtable in http://en.wikipedia.org/wiki/Vtable
But I don't understand why the reference to `vtable for KL' is undefined.

If KL::press() is defined inline within the KL.h file, there is no compile error. Why is that?

Key.h
1
2
3
4
5
6
7
8
9
#ifndef Key_h
#define Key_h

class Key
{
    public:
	virtual void press()=0;
};
#endif 


KL.h
1
2
3
4
5
6
7
8
9
10
#ifndef KL_h
#define KL_h
#include "Key.h"

class KL: public Key
{
    public:
	void press();
};
#endif 


KL.cpp
1
2
3
4
5
#include "KL.h"

void KL::press()
{
}


tester.cpp
1
2
3
4
5
6
7
8
#include "KL.h"
using namespace std;

KL kl;

int main()
{
}


Thank you.
Last edited on
I don't see you building the KL.cpp file, and link it to the output of tester.cpp. If you don't link it, KL::press is not implemented. See for example http://stackoverflow.com/questions/3065154/undefined-reference-to-vtable
Thank you ats15. You were right. This Makefile compiled it:
1
2
3
4
5
6
7
8
9
10
all: tester.exe

tester.exe: tester.o KL.o
	g++ -o tester.exe tester.o KL.o

tester.o: tester.cpp
	g++ -c tester.cpp

KL.o: KL.cpp KL.h Key.h
	g++ -c KL.cpp
Topic archived. No new replies allowed.