#include
using namespace std;
class R{
public :
int n;
int d;
R(int a,int b)
{
this->n=a;
this->d=b;
}
};
ostream operator<< (ostream &os,R &r)
{
// os<<r.n<<endl;
// os<<r.d<<endl;;
os<<r.n<<','<<r.d;
return os; //此處報錯了 在vs2015中做的
}
int main()
{
R a(2,3),b(4,5);
cout<<a<<b;
return 0;
}
這是我寫的一個重載輸入輸出去處符的小例子, 你參考一下吧
class Test
{
public:
int a;
int b;
Test():a(0),b(0){}
Test(int a, int b):a(a),b(b){}
//重載輸入輸出運算符,只能用友元函數
friend ostream &operator<<(ostream &os, const Test &T);
friend istream &operator>>(istream &is, Test &T);
};
ostream &operator<<(ostream &os, const Test &T)
{
os << "a = " << T.a << endl;
os << "b = " << T.b << endl;
return os;
}
istream &operator>>(istream &is, Test &T)
{
is >> T.a >> T.b;
return is;
}