我有一个班级:
class Optimizer {
public:
Optimizer(mongoc_client_t *dbClient, std::string strategyName, std::string symbol, int group);
virtual ~Optimizer() = 0;
及其实施:
#include "optimizers/optimizer.h"
Optimizer::Optimizer(mongoc_client_t *dbClient, std::string strategyName, std::string symbol, int group) {
// ...
}
Optimizer::~Optimizer() {
free(data);
}
但是我得到以下错误,似乎与析构函数未定义有关:
Undefined symbols for architecture x86_64:
"typeinfo for Optimizer", referenced from:
typeinfo for ReversalsOptimizer in reversalsOptimizer.o
"vtable for Optimizer", referenced from:
Optimizer::Optimizer(_mongoc_client_t*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int) in optimizer.o
Optimizer::~Optimizer() in optimizer.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [prepareData] Error 1
我也试过让析构函数非虚拟,但它没有帮助。
答案 0 :(得分:2)
问题正是错误信息所说的:
注意:缺少vtable通常意味着第一个非内联虚拟成员函数没有定义。
具体来说,您声明并调用但从未定义过此函数:
virtual void prepareStudies();
答案 1 :(得分:0)
事实证明,使Optimizer类中的其他虚函数成为纯虚函数解决了这个问题。