#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;
}
#include
using namespace std;
class num
{
public:
num(){n=1;}//构造函数
~num(){} //析构函数
int get() const{return n;}
void set(int x){n=x;}
void add(){++n;}
void operator++(){++n;}//没有返回值,调用重载运算符oper......
答案就在这里:【c++程序】运算符的重载
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。
friend ostream operator<< (ostream &os,R &r)
否则无法访问私有成员