sinat_16985427
sinat_16985427
采纳率0%
2015-09-20 04:06 浏览 2.7k

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答 默认 最新

  • CSDNXIAOC Robot-C 2015-09-20 04:10

     
    用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,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    点赞 评论 复制链接分享
  • oyljerry oyljerry 2015-09-20 05:11

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

    点赞 评论 复制链接分享
  • mo_fan_qing_wa modelfrog 2015-09-20 08:12

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

    点赞 评论 复制链接分享

相关推荐