类模板的.h和.cpp文件分离

来源:互联网 发布:旧版手机淘宝2014安卓 编辑:程序博客网 时间:2024/05/16 18:49
template<class T>class Abc{public:T a;public:void setvv(T b);T getvv();};

#include "b.h"template<class T>void Abc<T>::setvv(T b){a = b;}template<class T>T Abc<T>::getvv(){return a;}

#include "b.cpp"#include <iostream>using namespace std;int main(){Abc<int> ab;ab.setvv(10);cout<<ab.getvv()<<endl;return 0;}

1.在使用以.h,.cpp分离实现模板类时,不能像使用普通类一样只简单的包涵.h头文件,应该在使用模板类的cpp文件中引入模板类相应的cpp文件
2.将模板类的声明与实现都放在.h中(在多个cpp中使用不同模板参数时可能会引起重复定义的编译错误)

原创粉丝点击