Jam*_*ook 22 c++ linker templates
我知道我在这里错过了一些简单的东西,但我有一个模板化的成员函数,这是我专门的.
MyClass
{
template<typename T> T GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s);
}
template<typename T>
T MyClass::GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s)
{
throw std::runtime_error("Don't know how to convert " + ToString(v->GetString()));
}
template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> v, string s)
{
return v->GetInteger();
}
template<>
string MyClass::GetTFromVariable<string>(shared_ptr<TOtSimpleVariable> v, string s)
{
return v->GetString();
}
// etc for other specialisations.
Run Code Online (Sandbox Code Playgroud)
这是在我的头文件中定义的(因为模板应该是)但是当我去编译时我得到了一堆多重定义的符号,代表这样的错误是:
OtCustomZenith_logic.lib(PtPathOutput.obj) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall MyClass::GetTFromVariable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class boost::shared_ptr<class TOtSimpleVariable>,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$GetTFromVariable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@CommandProperties@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VTOtSimpleVariable@@@boost@@V12@@Z) already defined in TableFareSystem_test.obj
Run Code Online (Sandbox Code Playgroud)
我可以通过内联方法来修复它,但我不认为这应该是必要的......我忘记了什么?
编辑:我正在使用Visual Studio 2010
Che*_*Alf 31
完全专业化不再是模板.这是一个具体的功能.因此,它需要(隐式或显式)声明inline.最简单的方法是在返回类型规范之前添加该关键字.
也就是说,显示的代码与您的错误消息不对应.
您的错误消息是关于返回类型std::string,而不是返回类型int.
干杯&hth.,
Kei*_*ith 29
正如Alf指出的那样,完全专业化不再是模板.但是,我不确定它是否必须内联定义.您还应该能够拆分声明和定义.
即你的标题有:
template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> v, string s);
Run Code Online (Sandbox Code Playgroud)
并在实施中有:
template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> v, string s)
{
return v->GetInteger();
}
Run Code Online (Sandbox Code Playgroud)
我也曾经认为,通过权限,模板化的定义也应该是明确的内联(我一直这样做)但如果给定的编译器在应用模板的ODR时松懈,也不会太惊讶.我有兴趣看到另外说明的标准参考.
| 归档时间: |
|
| 查看次数: |
10824 次 |
| 最近记录: |