我们知道模板函数或模板类的定义一般都是和声明一起在头文件中,但是这样的话, 就暴露了内部实现,有什么办法能够将定义和声明进行分离呢?

答案是: 有的;

头文件: test.h;
class test
{
   template<class T> void f(T  &val);  
}

cpp文件: test.cpp;
#include "test.h"

template<T>
void  test::f(T &val){
      .....    
}

// 要将定义和声明分开,可以通过实例化声明来实现;  注意: 实例化在cpp文件中;
template void test::f<int>(int val);
template void test::f<double>(double val);


这样,在进行调用时,就不会报找不到定义的错误了;

  

相关文章:

  • 2021-07-25
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
猜你喜欢
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2021-07-06
  • 2022-12-23
相关资源
相似解决方案