Dec 22, 2013 at 5:09pm UTC
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 Dec 22, 2013 at 5:56pm UTC