友元成员函数的问题

langzimaomao 2003-08-20 01:22:43
#include<iostream.h>
class complex
{
private:
double real;
double imag;
public:
complex(double r=0,double i=0);
void print()
friend complex operator +(complex a,complex b);
};
complex::complex(double r,double i)
{
real=r;
imag=i;
}
complex operator +(complex a,complex b)
{
complex temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
void complex::print()
{
cout<<real;
if(imag>0)
cout<<"+";
if(imag!=0)
cout<<imag<<"i"<<endl;
}
main()
{
complex x1(3,5),x2(5,9),x3;
x3=x1+x2;
x3.print();
return 0;
}

此程序在类内的void print()加个";",便可以运行。
但print()后没有";",程序便有八个错误。
“ cpp(20) : error C2248: 'real' : cannot access private member declared in class 'complex' ”
怎么分析这个错误?

print()函数应该和下面的friend operator+(complex a,complex b)函数没有联系
即使少了个“;”,也只是语法错误。这是怎么回事?


...全文
59 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fireseed 2003-08-20
  • 打赏
  • 举报
回复
没有;号则下面的那句friend complex operator +(complex a,complex b);被当做声明,所以无效,所以这个函数没有被成功的声明为那个类的友员。

即然这个函数不是complex类的友员,当然不能访问complex类的私有成员了
cdxiaogan 2003-08-20
  • 打赏
  • 举报
回复
“print()函数应该和下面的friend operator+(complex a,complex b)函数没有联系
即使少了个“;”,也只是语法错误。这是怎么回事?”

因为少了个“;”所以编译时没有把operator +(complex a,complex b)识别为友元函数,甚至就没发现类里面有这么一个声明。所以编译器只把operator+()作为外部的一般函数处理,自然会出现那些不可访问的错误
这个与具体的编译平台有关
pengzhenwanli 2003-08-20
  • 打赏
  • 举报
回复
这就是编译器的问题了。
我的编译器给出下面的错误。你看看
main.cpp
g:\Project\Test\main.cpp(16) : warning C4518: 'friend ' : storage-class or type specifier(s) unexpected here; ignored
g:\Project\Test\main.cpp(16) : error C2146: syntax error : missing ';' before identifier 'complex'
g:\Project\Test\main.cpp(16) : error C2804: binary 'operator +' has too many parameters
g:\Project\Test\main.cpp(26) : error C2248: 'complex::real' : cannot access private member declared in class 'complex'
g:\Project\Test\main.cpp(11) : see declaration of 'complex::real'
g:\Project\Test\main.cpp(9) : see declaration of 'complex'
g:\Project\Test\main.cpp(26) : error C2248: 'complex::real' : cannot access private member declared in class 'complex'
g:\Project\Test\main.cpp(11) : see declaration of 'complex::real'
g:\Project\Test\main.cpp(9) : see declaration of 'complex'
g:\Project\Test\main.cpp(26) : error C2248: 'complex::real' : cannot access private member declared in class 'complex'
g:\Project\Test\main.cpp(11) : see declaration of 'complex::real'
g:\Project\Test\main.cpp(9) : see declaration of 'complex'
g:\Project\Test\main.cpp(27) : error C2248: 'complex::imag' : cannot access private member declared in class 'complex'
g:\Project\Test\main.cpp(12) : see declaration of 'complex::imag'
g:\Project\Test\main.cpp(9) : see declaration of 'complex'
g:\Project\Test\main.cpp(27) : error C2248: 'complex::imag' : cannot access private member declared in class 'complex'
g:\Project\Test\main.cpp(12) : see declaration of 'complex::imag'
g:\Project\Test\main.cpp(9) : see declaration of 'complex'
g:\Project\Test\main.cpp(27) : error C2248: 'complex::imag' : cannot access private member declared in class 'complex'
g:\Project\Test\main.cpp(12) : see declaration of 'complex::imag'
g:\Project\Test\main.cpp(9) : see declaration of 'complex'

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧