cannot access private member declared in class

#include
using namespace std;
class Complex{
public:
friend ostream&operator << (ostream&,Complex&);//声明重载运算符<<
friend istream&operator >> (istream&,Complex&);//声明重载运算符>>
private:
double real;
double imag;
};
ostream& operator <<(ostream& output,Complex &c)//定义重载运算符<<函数
{
output<<"("< }
istream& operator >函数
{
cout<<"input real part and imaginary part of complex number:"
input>>c.real>>c.imag;
return input;
}
int main()
{
Complex c1,c2;
cin>>c1>>c2;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
return 0;
}
vc下提示的错误

C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(305) : error C2248: 'real' : cannot access private member declared in class 'Complex'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(300) : see declaration of 'real'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(305) : error C2248: 'imag' : cannot access private member declared in class 'Complex'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(301) : see declaration of 'imag'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(310) : error C2146: syntax error : missing ';' before identifier 'input'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(310) : error C2248: 'real' : cannot access private member declared in class 'Complex'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(300) : see declaration of 'real'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(310) : error C2248: 'imag' : cannot access private member declared in class 'Complex'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(301) : see declaration of 'imag'
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(317) : error C2593: 'operator <<' is ambiguous
C:\Users\Administrator\Desktop\c++\运算符重载\main.cpp(318) : error C2593: 'operator <<' is ambiguous

ios

3个回答

 
用sort进行排序的时候遇到了这个错误,上网一搜,发现了原因所在。又学到东西了,真是庆幸至极。
 
 
1>e:/program files/microsoft visual studio 9.0/vc/atlmfc/include/afxtempl.h(776) : error C2248: 'CObject::operator......
答案就在这里:error C2248: 'CObject::operator =' : cannot access private member declared in class
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

private私有变量不能类外访问,要改成public类型。

限定修饰符选错了,把private换成public试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐