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