我不懂得问题关于友元的问题

wangdon0475cn 2004-12-03 01:17:40
有一段程序,如下:
#include <iostream.h>
//using namespace std;

class CL
{
public:
int count;
CL operator=(CL obj);
friend CL operator+(CL ob, int i);
friend CL operator+(int i, CL ob);
};

CL CL::operator=(CL obj)
{
count = obj.count;
return *this;
}

//处理ob+int的运算
CL operator+(CL ob, int i)
{
CL temp;
temp.count = ob.count + i;
return temp;
}

//处理int +ob的运算
CL operator+(int i, CL ob)
{
CL temp;
temp.count = ob.count + i;
return temp;
}

int main()
{
CL o;
o.count = 10;
cout << o.count << " ";

o = 10 + o;
cout << o.count << " ";

o = o + 12;
cout << o.count;


return 0;
}
这么编译没有错误,但是我把头两行改为
#include <iostream>
using namespace std;
编译后出现以下错误:
--------------------Configuration: 友元函数重载1 - Win32 Debug--------------------
Compiling...
友元函数重载1.cpp
d:\documents and settings\administrator\桌面\友元函数重载1.cpp(9) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
友元函数重载1.obj - 1 error(s), 0 warning(s)

我想问,按照现在标准我的第二种写法没有错误,是不是哪里有问题,我是弄不懂了,我的C++很菜,还希望高手指点一二!
...全文
303 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
greenteanet 2004-12-03
  • 打赏
  • 举报
回复
上面我把friend注释掉啦,可以在VC6.0下通过。
后来,我按楼主的意思,在VC2003.NET下调试,没有出现任何问题.也就是说那是编译器的问题。
greenteanet 2004-12-03
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class CL
{
public:
int count;
CL operator=(CL obj);

//friend CL operator+(CL ob, int i);
//friend CL operator+(int i, CL ob);
};

CL CL::operator=(CL obj)
{
count = obj.count;
return *this;
}

//处理ob+int的运算
CL operator+(CL ob, int i)
{
CL temp;
temp.count = ob.count + i;
return temp;
}

//处理int +ob的运算
CL operator+(int i, CL ob)
{
CL temp;
temp.count = ob.count + i;
return temp;
}

int main()
{
CL o;
o.count = 10;
cout << o.count << " ";

o = 10 + o;
cout << o.count << " ";

o = o + 12;
cout << o.count;


return 0;
}

qnjian 2004-12-03
  • 打赏
  • 举报
回复
真巧,俺也发现这个问题了,是在昨天!同苦,同苦!呵呵
dudu妈 2004-12-03
  • 打赏
  • 举报
回复
我今天也遇到这样等问题,在devc++中编译也是没有出现问题,但是到了vc6就出现错误,而且错误就出现在友元函数那里,错误就是error C1001:, 经过几个同事得研究和在网络上搜索,发现就是编译器得问题。刚好今天我也发现这个问题了,哈哈哈太巧了!!!
zhangfjj 2004-12-03
  • 打赏
  • 举报
回复
这个问题我解答过几次了。
最近正好在弄运算符重载,前一阵在写一个例程,复数类运算符的重载,用VC6不行(我的是创天中文版。同样,出现了这个该死的fatal error C1001:。
用dev-c++可以通过,几乎让我认为VC6不支持友元重载运算符。不甘心,我在网上找,终于让我找着了,这个问题跟编译器有关。
解决的办法是在前面加类的前导声明和友元的前导声明。


这段代码的调试,在dev-c++中是可以通过,但在vc6不行,老是出现
fatal error C1001: INTERNAL COMPILER ERROR(compiler file 'msc1.cpp', line 1786) Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more informationError executing cl.exe.
在网上查了一下资料,这里有一个前导声明的问题(限于友元方式重载运算符)
需要在类声明前面进行前导类的声明和友元函数的声明。
即:前面2行,大家在编使用vc编译器时要注意。
class MyComplex;
MyComplex operator + (MyComplex& C1,MyComplex& C2);
/*在使用友元重载运算符的时候,需要在类声明前面加上前导类的声明和函数的声明*/
class MyComplex
{
double re,im;
public:
MyComplex():re(0),im(0){}
MyComplex(double m_re,double m_im)
{
re=m_re;
im=m_im;
}
friend MyComplex operator + (MyComplex& C1,MyComplex& C2);
void Display()
{
cout<<re<<"+"<<im<<"i"<<endl;
}
};
MyComplex operator+ (MyComplex& C1,MyComplex& C2)
{
double rre=C1.re+C2.re;
double rim=C1.im+C2.im;
MyComplex result(rre,rim);
return result;
}
Alanbus 2004-12-03
  • 打赏
  • 举报
回复
hehe,问题出在#include <iostream.h>,改为#include <iostream>即可,在VC6和dev中均通过编译
oyljerry 2004-12-03
  • 打赏
  • 举报
回复
友元函数看出全局函数
zhouyong0371 2004-12-03
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class CL
{
public:
int count;
CL operator=(CL obj);
//friend CL operator+(CL ob, int i);
//friend CL operator+(int i, CL ob);
};

CL CL::operator=(CL obj)
{
count = obj.count;
return *this;
}

//处理ob+int的运算
CL operator+(CL ob, int i)
{
CL temp;
temp.count = ob.count + i;
return temp;
}

//处理int +ob的运算
CL operator+(int i, CL ob)
{
CL temp;
temp.count = ob.count + i;
return temp;
}

int main()
{
CL o;
o.count = 10;
cout << o.count << " ";

o = 10 + o;
cout << o.count << " ";

o = o + 12;
cout << o.count;


return 0;
}
carylin 2004-12-03
  • 打赏
  • 举报
回复
在我的機子上沒有問題。

可以將函數
CL operator+(int i, CL ob)
{
CL temp;
temp.count = ob.count + i;
return temp;
}
寫成這樣:
CL operator+(int i, CL ob)
{
return operator+(ob, i);
}
goodluckyxl 2004-12-03
  • 打赏
  • 举报
回复
友员类似全局函数了

普通友员在作用域外的看不到 模板比较特殊

去掉 类::这个东西 当作普通函数定义

64,701

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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