P
pipehappy
Hi there,
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:
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:
#ifndef HE
#define HE
class Base {
public:
virtual double func(double)=0;
};
class Child: public Base{
public:
virtual double func(double);
};
#endif
a.cpp:
#include "a.h"
double Child::func(double a) {
return a+ 3;
}
main.cpp:
#include "a.h"
int
main (void) {
Child child1;
child1.func(0.3);
}
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:
g++ -c a.cpp -o liba.o
ar rcs liba.a liba.o
g++ -L. -la main.cpp -o main
/tmp/ccqQZO7f.o
reference to `__cxa_pure_virtual'
Anyone has any suggestion about this?
Thx
Here is the code example:
a.h:
#ifndef HE
#define HE
class Base {
public:
virtual double func(double)=0;
};
class Child: public Base{
public:
virtual double func(double);
};
#endif
a.cpp:
#include "a.h"
double Child::func(double a) {
return a+ 3;
}
main.cpp:
#include "a.h"
int
main (void) {
Child child1;
child1.func(0.3);
}