I have came across a problem to make a library with virtual function.
I have example code listed below and if I compile this way it works: g++ main.cpp a.cpp -o main
But when I want to have a static library it reports error:
1 2 3 4
g++ -c a.cpp -o liba.o
ar rcs liba.a liba.o
g++ -L. -la main.cpp -o main
/tmp/ccqQZO7f.o:(.rodata._ZTV4Base[vtable for Base]+0x10): undefined reference to `__cxa_pure_virtual'
Anyone has any suggestion about this?
Thx
Here is the code example:
a.h:
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef HE
#define HE
class Base {
public:
virtualdouble func(double)=0;
};
class Child: public Base{
public:
virtualdouble func(double);
};
#endif
a.cpp:
1 2 3 4 5
#include "a.h"
double Child::func(double a) {
return a+ 3;
}
main.cpp:
1 2 3 4 5 6 7
#include "a.h"
int
main (void) {
Child child1;
child1.func(0.3);
}
write the function definition for the func function for the base class - even though it is pure virtual.
Should stop the compiler complaining.
I know it sounds weird - but if you have a pure virtual function which makes the class an abstract class and you therefore cannot create instances of the class - you can still give the function a body
(and you can call the function )