数学建模社区-数学中国
标题:
求助:类模版==的操作符重载,请大家教教我这代码错哪了
[打印本页]
作者:
且生
时间:
2013-2-26 14:01
标题:
求助:类模版==的操作符重载,请大家教教我这代码错哪了
#include<iostream>
5 t$ y* B6 x6 |# d% [( T P
using namespace std;
6 v$ F/ N4 t$ _6 l+ D
template<class T>
1 e, I3 q. e5 C& T1 r% Z8 `
class Sample
u) E7 |3 s5 f8 V9 A
{
) Q# G) T6 ]% |* n M
private:
, f" Y7 n( U+ Z9 o3 g7 a
T n;
8 Y9 W$ a8 M: B
public:
; {! c( s) k8 k) x9 Z
Sample():n(0){}
$ w2 ]/ s' \5 a5 r4 h
Sample(T a):n(a){}
: A m/ A* P% h. q; w
friend bool operator== (const Sample<T>&,const Sample<T>&);
5 m8 i2 {" f% A/ ~
};
1 h% d7 e0 k! _& ~1 E
2 e2 e$ h6 _) m4 Z, f
int main()
4 F! Z( ^" N4 _7 [, |) F6 Q$ z" F
{
?, a3 M: o3 |6 B5 @5 v z
Sample<int> a(10),b(2);
# x/ U! Q) I' ]) ~; Z4 v8 O* }4 b
if(a==b)cout<<"true";
, ^; J& H4 L% d2 m" z
else cout<<"false";
+ h& S" W( r1 D4 W
cout<<endl;
* _' E- x A3 D1 S" P. P
system("pause");
2 |0 z2 \5 O* @
return 0;
' e2 o6 _) G# Z# e r
}
- F* s, z" K6 z* G B
template<class T>
9 I# U X) e* C& G Y, V
bool operator==(const Sample<T>& a,const Sample<T>& b)
2 q$ `( w$ k5 \% n; I# B$ N
{
5 i8 t9 o7 D6 H! g
if(a.n==b.n)
% E4 ^: N0 Z, W3 j
return true;
( }8 h* b' J$ ?5 c6 s/ g: d5 n0 F
else return false;
( w& D, |, B8 `' z K- E
}
复制代码
==用来比较两个数。
作者:
wangzheng3056
时间:
2013-7-22 11:49
楼主解决了吗? 跪求科普~!
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5