0
// The original code is from link: http://hoeven.blogbus.com/logs/37324287.html
#include<iostream>
#include<vector>
#include <stdlib.h>

using namespace std;

class test{
public:
     int v;
   /*构造函数*/
     test():v(0){}
     test(const int &a):v(a){}
     test(const test &t1):v(t1.v){}

   /*以下重载小于号 < */
     //比较两个对象的大小
     bool operator<(const test &t1) const{
         return (v < t1.v);
     }
     //比较对象和int的大小
     bool operator<(const int &t1) const{
         return (v < t1);
     }
     //友元函数,比较int和对象的大小
     friend inline bool operator<(const int &a, const test & t1){
         return (a < t1.v);
     }

   /*以下重载赋值号 = */
     //对象间赋值
     test & operator=(const test &t1){
         v = t1.v;
         return *this;
     }
     //int赋值给对象
     test & operator=(const int &t1){
         v = t1;
         return *this;
     }

   /*以下重载加号 + */
     //对象加上 int
     test operator+(const int & a){
         test t1;
         t1.v = v + a;
         return t1;
     }
     //对象加对象
     test operator+(test &t1){
         test t2;
         t2.v = v + t1.v;
         return t2;
     }

   /*以下重载加等号 += */  
     //对象加上对象
     test &operator+=(const test &t1){
         v += t1.v;
         return *this;
     }  
     //对象加上int
     test &operator+=(const int &a){
         v += a;
         return *this;
     }

   /*以下重载双等号 == */  
     //对象==对象
     bool operator==(const test &t1)const{
         return (v == t1.v);
     }  
     //对象==int
     bool operator==(const int &t1)const{
         return (v == t1);
     }  

   /*以下重载 输入>> 输出<< */
     /*友元函数,输出对象*/
     friend inline ostream & operator << (ostream & os, test &t1){
         cout << "class t(" << t1.v << ")" << endl;
         return os;
     }
     /*友元函数,输入对象*/
     friend inline istream & operator >> (istream & is, test &t1){
         cin >> t1.v;
         return is;
     }
};

int main(){
     test t0, t1(3);  // t0 has no initial value, so use default value 0
     test t2(t1);
     cout << t0 << t1 << t2;
     cin >> t1;
     t2 = t1;
     t2 += t1;
     t1 += 10;
     cout << t2;
     if(t1 < t2) cout << "t1 < t2";
     else if(t1 == t2) cout << "t1 = t2";
     else /* t1 > t2*/ cout << "t1 > t2";
     cout <<endl;
     system("echo Tom");
     return 0;
}

/*
 $ ./a.out 
 class t(0)
 class t(3)
 class t(3)
 45
 class t(90)
 t1 < t2
 Tom
 */

完全なコードは上記です。しかし、なぜ「ostream & os」(以下を参照) が括弧内になければならないのか理解できません。「ostream & os」を削除すると、大量のエラーが発生しました。

 friend inline ostream & operator << (ostream & os, test &t1){
     cout << "class t(" << t1.v << ")" << endl;
     return os;
 }
4

5 に答える 5

7

貼り付けるコードが間違っているためです。

<< 演算子はそのパラメーターに書き込む必要があります。つまり、あなたの機能は

inline ostream & operator << (ostream &os, test &t1) {
    os << "class t(" << t1.v << ")" << endl;
    return os;
}

これにより、.ostream だけでなく、任意の ostream に書き込むことができますcout。前のコードでは、 ofile << t1;(ファイルストリームであると考えてofile)書き込んだ場合、これはファイルへの書き込みではなく、標準出力に書き込まれます。

于 2012-11-15T09:39:20.167 に答える
3

オーバーロードには 2 つの方法がありますoperator<<

  1. 単項( this->operator<<(other_object)),
  2. バイナリ( operator<<(ostream& os, other_object)。

それらの1つだけが可能です(2番目)。最初は不可能です。これを実装するには、ostreamクラスoperator <<の関数をオーバーロードする必要があるためです。これはできません。クラスのオブジェクトと別のクラスのオブジェクトを引数として取得する二項演算子が呼び出される
ようなステートメントを記述すると、cout << "some text"; ostream

于 2012-11-15T09:44:07.717 に答える
1

メソッドに変数を渡した場合にのみostream、メソッドは出力先を認識します。または、ファイル ストリームを開いてメソッドに渡すこともできます。次に、出力は標準出力ではなくファイルに書き込まれます。

于 2012-11-15T09:51:17.753 に答える
1

はのostream & os左側operator <<です。すなわちcout << myObject;。この場合、coutですostream。ただし、オーバーロードでは、os変数を無視してcout無条件に使用しています。coutオーバーロードでに置き換える必要がosありますoperator <<

于 2012-11-15T09:38:07.033 に答える
1

ostream 引数を使用すると、出力先をさらに柔軟に指定できます。また、同じステートメントで複数のアイテムを出力できるようにチェーンすることもできます。

test t1;
test t2(5);
test t3(-4);

// Write "0 5 -4\n" to standard output
cout << t1 << " " << t2 << " " << t3 << endl;

// Write "0 5 -4\n" to the file my_file.txt
ofstream ofs("my_file.txt");
ofs << t1 << " " << t2 << " " << t3 << endl;

// Write "0 5 -4\n" to oss, which is then written to standard output
ostringstream oss;
oss << t1 << " " << t2 << " " << t3 << endl;
cout << oss.str() << endl;
于 2012-12-16T17:44:43.010 に答える