为什么在 C++ 中调用此虚函数会导致基 class 的 'missing symbol' 错误?
Why does calling this virtual function in C++ result in a 'missing symbol' error from the base class?
我有一个虚拟的 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);
}
};
注意。注意 Clone:
的用法
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。
怎么回事?
为什么调用函数时派生 class 中的实现没有自动解析?
是不是因为这是一个模板化的方法?
完整代码可以在这个要点中找到,供参考:https://gist.github.com/shadowmint/d49650668e9a74c324a1
如果你不想实现基class的虚成员函数,你必须声明它是纯虚的:
// vvv-- here
virtual Resource<TId, TValue> *Clone() = 0;
否则,链接器将搜索您声明但在为基础 class 生成虚函数 table 时未实现的函数(您可以在错误消息中看到).
请注意,在其中声明一个纯虚成员函数将生成一个 class 抽象。
我有一个虚拟的 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);
}
};
注意。注意 Clone:
的用法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。
怎么回事?
为什么调用函数时派生 class 中的实现没有自动解析?
是不是因为这是一个模板化的方法?
完整代码可以在这个要点中找到,供参考:https://gist.github.com/shadowmint/d49650668e9a74c324a1
如果你不想实现基class的虚成员函数,你必须声明它是纯虚的:
// vvv-- here
virtual Resource<TId, TValue> *Clone() = 0;
否则,链接器将搜索您声明但在为基础 class 生成虚函数 table 时未实现的函数(您可以在错误消息中看到).
请注意,在其中声明一个纯虚成员函数将生成一个 class 抽象。