数学建模社区-数学中国

标题: 求助:类模版==的操作符重载,请大家教教我这代码错哪了 [打印本页]

作者: 且生    时间: 2013-2-26 14:01
标题: 求助:类模版==的操作符重载,请大家教教我这代码错哪了
  1. #include<iostream>5 t$ y* B6 x6 |# d% [( T  P
  2. using namespace std;6 v$ F/ N4 t$ _6 l+ D
  3. template<class T>1 e, I3 q. e5 C& T1 r% Z8 `
  4. class Sample
      u) E7 |3 s5 f8 V9 A
  5. {
    ) Q# G) T6 ]% |* n  M
  6. private:
    , f" Y7 n( U+ Z9 o3 g7 a
  7.         T n;8 Y9 W$ a8 M: B
  8. public:; {! c( s) k8 k) x9 Z
  9.         Sample():n(0){}
    $ w2 ]/ s' \5 a5 r4 h
  10.         Sample(T a):n(a){}
    : A  m/ A* P% h. q; w
  11.         friend bool operator== (const Sample<T>&,const Sample<T>&);
    5 m8 i2 {" f% A/ ~
  12. };
    1 h% d7 e0 k! _& ~1 E

  13. 2 e2 e$ h6 _) m4 Z, f
  14. int main()
    4 F! Z( ^" N4 _7 [, |) F6 Q$ z" F
  15. {
      ?, a3 M: o3 |6 B5 @5 v  z
  16.         Sample<int> a(10),b(2);# x/ U! Q) I' ]) ~; Z4 v8 O* }4 b
  17.         if(a==b)cout<<"true";, ^; J& H4 L% d2 m" z
  18.         else cout<<"false";+ h& S" W( r1 D4 W
  19.         cout<<endl;* _' E- x  A3 D1 S" P. P
  20.         system("pause");2 |0 z2 \5 O* @
  21.         return 0;' e2 o6 _) G# Z# e  r
  22. }- F* s, z" K6 z* G  B
  23. template<class T>9 I# U  X) e* C& G  Y, V
  24. bool operator==(const Sample<T>& a,const Sample<T>& b)
    2 q$ `( w$ k5 \% n; I# B$ N
  25. {5 i8 t9 o7 D6 H! g
  26.         if(a.n==b.n)
    % E4 ^: N0 Z, W3 j
  27.                 return true;( }8 h* b' J$ ?5 c6 s/ g: d5 n0 F
  28.         else return false;
    ( w& D, |, B8 `' z  K- E
  29. }
复制代码
==用来比较两个数。
作者: wangzheng3056    时间: 2013-7-22 11:49
楼主解决了吗?  跪求科普~!




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5