我有一个完美的代码:
template <typename ...Ts>
class ThreadImplementation {
...
void launch(){...}
~ThreadImplementation(){...}
};
...
ThreadImplementation<Ts...> *t = new ThreadImplementation<Ts...>(debugName,func,args...);
...
// std::vector< std::unique_ptr< ThreadImplementation<> > > thread_107_allThreads;
thread_107_allThreads.emplace_back(t);
...
其中thread_107_allThreads现在仅存储没有模板参数的那些,因此ThreadImplementation&lt;&gt;在声明中
现在如果我加上
class AnyThreadImplementation {
public:
virtual void launch() = 0;
virtual ~AnyThreadImplementation() = 0;
};
并且仅进行以下3次更改(不进行投射,不使用更改,也不进行测试):
class ThreadImplementation : public AnyThreadImplementation {
virtual void launch(){...}
virtual ~ThreadImplementation(){...}
(尽管最后2个更改不会影响结果,即:如果我错过了子类中实际实现之前的虚拟,我会得到相同的错误)
我得到了:
/tmp/test-fuPonc.o: In function `_ZN20ThreadImplementationIJEEC2EPKcPFvvE':
test.cpp:(.text._ZN20ThreadImplementationIJEEC2EPKcPFvvE[_ZN20ThreadImplementationIJEEC2EPKcPFvvE]+0xbb): undefined reference to `AnyThreadImplementation::~AnyThreadImplementation()'
/tmp/test-fuPonc.o: In function `_ZN20ThreadImplementationIJEED2Ev':
test.cpp:(.text._ZN20ThreadImplementationIJEED2Ev[_ZN20ThreadImplementationIJEED2Ev]+0x233): undefined reference to `AnyThreadImplementation::~AnyThreadImplementation()'
test.cpp:(.text._ZN20ThreadImplementationIJEED2Ev[_ZN20ThreadImplementationIJEED2Ev]+0x267): undefined reference to `AnyThreadImplementation::~AnyThreadImplementation()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
为什么?
答案 0 :(得分:1)
也应该定义纯虚拟析构函数。
N3376 12.4 / 9的相关报价
析构函数可以声明为虚拟或纯虚拟; 如果该类的任何对象或任何 派生类是在程序中创建的,应该定义析构函数。
class AnyThreadImplementation {
public:
virtual void launch() = 0;
virtual ~AnyThreadImplementation() = 0;
};
AnyThreadImplementation::~AnyThreadImplementation() {}