c++问题,在运算符重载中,当定义一个私有的变量,定义友元会出错(我用的是vc++6.0)。
class person
{
friend ostream& operator<<(ostream& out, Person& p);
public:
Person(int a,int b)
{
this->m_A=a;
this->m_B=b;
}
private:
int m_a;
int m_b;
};
ostream& operator<<(ostream& cout,Person& p)
{
cout<<“a:”<<p.m_A<<“b:”<<p.m_B;
return cout;
}
void test()
{
Person p(10,10);
cout<<p<<endl;
}
在第三行的友元定义,并不能让Person的私有成员给函数使用,请问出错在哪里,当把两个变量设为公共的,会出现重复定义运算符的错误。
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交