可能重复:
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)
任何想法,为什么我得到上述错误?
复制...不,你知道吗,我什至不打扰。 –