0
Possible Duplicate:
Why can templates only be implemented in the header file?C++模板不在头/链接错误定义
我只是碰到的东西来让我无法理解。 在以下情况下,我在链接阶段遇到问题。
//header file
class A
{
template<class T>
std::weak_ptr<T> GetSomethingFromSomeWhere(const char* Id);
};
//cpp file
template<class T>
std::weak_ptr<T> A:GetSomethingFromSomeWhere(const char* id)
{
//A method with the right stuff inside and the right return statement
...
}
//Another class
class B
{
};
//main.cpp
int main()
{
A a;
auto pB = a.GetSomethingFromSomeWhere<B>("id");
}
这并没有编译,链接我有这样的事情时:
用于建筑x86_64的未定义符号: “的std :: __ 1 :: weak_ptr的A :: GetComponentFromName(字符常量*) ”,从引用: _main在main.o中 LD:符号(多个)未找到架构x86_64的 铛:错误:连接器命令失败,退出代码1(使用-v看调用)
我固定它通过直接在头文件中定义模板方法。
我应该总是在头文件中定义模板方法吗?为什么? 我在OSX和使用铛++用的XCode如果能够帮上什么忙
感谢
我认为它可以使上联是自己的。我错了,谢谢你的链接。 – lollancf37