我有一个班:
C.h
class C { private: template<int i> void Func(); // a lot of other functions };
C.cpp
// a lot of other functions template<int i> void C::Func() { // the implementation } // a lot of other functions
解决方法
当以触发其实例化的方式使用函数模板时,编译器(在某些时候)需要看到该模板的定义.这就是原因,模板通常使用内联功能在头文件中实现.
所以只要遵守上述规则,仍然可以在头文件和源文件中分离接口和实现.
参考:
C 03标准,§14.7.2.4:
The definition of a non-exported function template,a non-exported member function template,or a non-exported member function or static data member of a class template shall be present in every translation unit in which it is explicitly instantiated.