我正在尝试以下代码片段:
请注意,我没有为类模板的成员函数实现任何内容。但这不是问题。我知道该怎么做!如果我尝试编译main.cpp,我应该会得到链接错误,这就是我得到的。问题是为什么它会尝试两次查找析构函数(下面错误的最后两行)。
GeneralTemplate.h
(这是一个标题)
#ifndef _GENERATEMPLATE_H_
#define _GENERATEMPLATE_H_
#include <iostream>
template <class T>
class GeneralTemplate
{
public:
GeneralTemplate();
GeneralTemplate(const GeneralTemplate &g);
~GeneralTemplate();
GeneralTemplate& operator= (GeneralTemplate const& g);
template <class M>
void arbitraryFunction(const M &m);
};
#endif
main.cpp
#include "GeneralTemplate.h"
#include <iostream>
int main()
{
GeneralTemplate<int> gInt;
gInt.arbitraryFunction(2.3);
return 0;
}
请注意,我没有为类模板的成员函数实现任何内容。但这不是问题。我知道该怎么做!如果我尝试编译main.cpp,我应该会得到链接错误,这就是我得到的。问题是为什么它会尝试两次查找析构函数(下面错误的最后两行)。
$g++ main.cpp
/tmp/cckrdPCs.o: In function `main':
main.cpp:(.text+0x13): undefined reference to `GeneralTemplate<int>::GeneralTemplate()'
main.cpp:(.text+0x34): undefined reference to `void GeneralTemplate<int>::arbitraryFunction<double>(double const&)'
main.cpp:(.text+0x45): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()'
main.cpp:(.text+0x61): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()'
collect2: ld returned 1 exit status
#include <iostream>
。</codereview> - Lightness Races in Orbitthrow()
(或者在C++11中使用noexcept
)标记该函数,仍然可能具有相同的行为,即不超过1个dtor调用。) - vsoftco