为什么在 C++ 中调用此虚函数会导致基类出现'missing symbol'错误?
Why does calling this virtual function in C++ result in a 'missing symbol' error from the base class?
>我有一个虚拟类,定义如下:
template<typename TId, typename TValue>
class Resource {
private:
/// Next item in the chain
unique_ptr<Resource<TId, TValue>> data;
protected:
/// The id this value
TId id;
/// The value on this value
TValue value;
public:
/// Create a new instance and assign the values to it
Resource(TId id, TValue value) {
this->id = id;
this->value = value;
}
/// Destructor
virtual ~Resource() {
}
/// Safely clone a new instance of this
virtual Resource<TId, TValue> *Clone();
... other stuff removed for brevity ...
/// Create a new resource chain with a custom filter
Option<Resource<TId, TValue>*> Filter(std::function< bool(Resource<TId, TValue>*) > filter) {
auto iter = this->Iter<Resource<TId, TValue>>(filter);
if (iter.Any()) {
auto index = 0;
auto root = (*iter.First())->Clone();
iter.Each([&] (Resource<TId, TValue>* r) {
if (index != 0) {
root->Push(r->Clone());
}
++index;
});
return Some<Resource<TId, TValue>*>(root);
}
return None<Resource<TId, TValue>*>();
}
};
我已经在测试中轻松实现了这一点,例如:
enum RType {
ThingOne = 1,
ThingTwo = 2
};
class RValue : public Resource<RType, i32> {
public:
RValue(int value) : Resource(ThingOne, value) {
}
~RValue() {
}
Resource<RType, i32>* Clone() {
return new RValue(this->value);
}
};
铌。注意克隆的用法:
root->Push(r->Clone());
但是,在编译时,我得到:
[ 88%] Building CXX object CMakeFiles/test-resource.dir/tests/test-resource.cpp.o
Linking CXX executable tests/test-resource
Undefined symbols for architecture x86_64:
"npp::Resource<RType, int>::Clone()", referenced from:
vtable for npp::Resource<RType, int> in test-resource.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [tests/test-resource] Error 1
make[1]: *** [CMakeFiles/test-resource.dir/all] Error 2
编译器是 clang,平台是 OSX。
这是怎么回事?
为什么在调用函数时,派生类中的实现不会自动解析?
是因为这是一个模板化的方法吗?
完整的代码可以在此要点中找到,以供参考:https://gist.github.com/shadowmint/d49650668e9a74c324a1
如果不想实现基类的虚拟成员函数,则必须将其声明为纯虚拟:
// vvv-- here
virtual Resource<TId, TValue> *Clone() = 0;
否则,链接器将搜索你在为基类生成虚拟函数表时声明但未实现的函数(可以在收到的错误消息中看到这一点)。
请注意,在其中声明纯虚拟成员函数将使类抽象。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 如何解决C++中声纳库贝静态代码分析错误"Explicitly define the missing copy constructor, move constructor .."
- 如何修复错误消息,如"argument list for class template is missing"?
- C++ "argument list for class template "迭代器" is missing"错误
- 我能做些什么"missing ; before X"一连串的错误?
- 在第 16,54,55,56,57,58 行中,我收到错误"Statement missing ; in function main()"为什么?
- 为什么在 C++ 中调用此虚函数会导致基类出现'missing symbol'错误?
- 叮当教程 "missing file"上的段错误
- 编译生成文件会产生'ld returned, DSO missing'错误
- 如何解决错误:"Missing required Boost components >= vX.XX, please install/upgrade Boost or configure you
- VC++ 2010:'missing type specifier'错误来来去去,无需更改代码
- 几条"Error C4430: missing type specifier"错误消息
- 错误"function call missing argument list" - 帮助为我指明正确的方向