下面的代码(这是我需要的简化版本)没有链接
在*.h文件中:
class InterfaceFunctionField2 {
public:
template<class outputType> outputType to() { return outputType(); }
};
在*.cpp文件中
template<> double InterfaceFunctionField2::to<double>()
{ return 3.; }
这个类位于一个静态库中。
我正在获取已在...中定义的"error LNK2005:"public: double __thiscall接口函数字段2::to(无效)常量“(??$to@N@接口函数字段2@@QBENXZ) ...”和“忽略第二个定义”警告LNK4006
我只定义了一次InterfaceFunctionField2::to()专门化,并且我没有#include *.cpp文件...
我已经在互联网(e.g. here)上查过了,这种类型的代码似乎还可以,但是链接器不同意。你能帮忙吗?谢谢。
发布于 2012-05-29 14:03:10
您还需要在标头中声明专门化。
//header.h
class InterfaceFunctionField2 {
public:
template<class outputType> outputType to() { return outputType(); }
};
template<> double InterfaceFunctionField2::to<double>();
//implementation.cc
template<> double InterfaceFunctionField2::to<double>()
{ return 3.; }
链接中的代码之所以有效,是因为专门化对该翻译单元是可见的。
https://stackoverflow.com/questions/10800751
复制