在项目中,我有一个虚拟课程:
class Base {
public:
Base();
virtual void insert(const Node& n);
virtual Node extract_min() = 0;
virtual int size() = 0;
virtual ~Base() {};
};
我派出了班级:
class Derived: public Base {
public:
derived(int MaxN = MAXN);
void insert(const Node& n);
Node extract_min();
int size() {
return cnt;
}
virtual ~Derived();
};
及其构造函数:
Derived::Derived(int MaxN): Base() {
//something happes here;
}
它的析构函数:
Derived::~Derived() {
free(my_array);
}
但是当我做到这一点时,我一直在链接阶段出现这个错误:
obj/Derived.o: In function `Derived::~Derived()':
Derived.cpp:(.text+0x160): undefined reference to `vtable for Base'
obj/Derived.o: In function `Derived::Derived(int)':
Derived.cpp:(.text+0x199): undefined reference to `Base::Base()'
Derived.cpp:(.text+0x516): undefined reference to `vtable for Base'
有人知道如何修复它吗?
答案 0 :(得分:1)
undefined reference to `vtable for Base'
此错误表示您忘记在Base类中提供(不纯)虚拟函数的定义。从其他错误看,你似乎错过了所有成员的定义。
答案 1 :(得分:-1)
您应该为基类添加ctor的定义。 这可以解决链接错误。
class Base {
public:
Base{};
virtual void insert(const Node& n);
virtual Node extract_min() = 0;
virtual int size() = 0;
virtual ~Base() {};
};
此外,您还添加了在派生类
中继承的虚函数的定义