可能重複:
Why can templates only be implemented in the header file?
Undefined reference to template members
c++ template and header files聲明一個namspace在函數模板中使用的一類
我正在使用的函數模板時得到一個編譯器錯誤一類。函數模板在獨立的源文件和頭文件中的名稱空間中聲明。
以下是一些示例代碼。
namespace A header file
namespace A {
...
...
template<typename T1, typename T2 >
bool OR(T1* j, vector<T2*>, float cut);
...
...
}
A cpp file
namespace A {
...
...
template<typename T1, typename T2 >
bool OR(T1* j, vector<T2*>, float cut){
....
....
}
}
然後我用這個命名空間中的一類
class B cpp file (only member function)
#include "A.h"
void B::exmaple(){
if(A::OR(m_ptrObj, m_ptrvectortoOtherObj, m_cut)){
cout << "its true" << endl;
}
}
所以編譯器錯誤,我得到如下所示(請注意,這是我真正的代碼中的錯誤,但同樣的想法)。
undefined reference to `bool JetFilters::OR<Jet, Jet>(Jet*, std::vector<Jet*, std::allocator<Jet*> >*, float)
任何想法,爲什麼我得到上述錯誤?
複製...不,你知道嗎,我什至不打擾。 –