c++模板

c++ 模板不需要分离。即所有东西写在 .h 文件中好了
网上看见的例子:


1 #include <iostream>
2
3 #include "ClassTemplate.h"
4
5 using std::cout;
6
7 using std::endl;
8
9 int main()
10
11 {
12
13 myClass<int,int> class1(3,5);
14
15 class1.show();
16
17 myClass<int,char> class2(3,'a');
18
19 class2.show();
20
21 myClass<double,int> class3(2.9,10);
22
23 class3.show();
24 return 1;
25
26 }

、、ClassTemplate.h :


#ifndef ClassTemplate_HH
2
3 #define ClassTemplate_HH
4 #include<iostream>
5 using namespace std;
6
7 template<typename T1,typename T2>
8
9 class myClass{
10
11 private:
12
13 T1 I;
14
15 T2 J;
16
17 public:
18
19 myClass(T1 a, T2 b);//Constructor
20
21 void show();
22
23 };
24
25 //这是构造函数
26
27 //注意这些格式
28
29 template <typename T1,typename T2>
30
31 myClass<T1,T2>::myClass(T1 a,T2 b):I(a),J(b){}
32
33 //这是void show();
34
35 template <typename T1,typename T2>
36
37 void myClass<T1,T2>::show()
38
39 {
40
41 cout<<"I="<<I<<", J="<<J<<endl;
42
43 }
44
45
46
47
48
49 #endif


g++ *.cpp \\ 就好了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值