| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 624 人关注过本帖
标题:运算符重载为友元函数的问题
只看楼主 加入收藏
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
结帖率:90.63%
收藏
已结贴  问题点数:10 回复次数:5 
运算符重载为友元函数的问题
#include <iostream>
using namespace std;
class Complex
{
public:
    Complex(double r=0.0, double i=0.0):real(r), imag(i) {}
    friend Complex operator+(const Complex &c1, const Complex &c2);
    friend Complex operator-(const Complex &c1, const Complex &c2);
    friend ostream &operator<<(ostream &out, const Complex &c);
private:
    double real;
    double imag;
};

Complex operator+(const Complex &c1, const Complex &c2)
{
    return Complex(c1.real+c2.real, c1.imag+c2.imag);
}
Complex operator-(const Complex &c1, const Complex &c2)
{
    return Complex(c1.real-c2.real, c1.imag-c2.imag);
}
ostream &operator<<(ostream &out, const Complex &c)
{
    out<<"("<<c.real<<","<<c.imag<<")";
    return out;
}
int main()
{
    Complex c1(5, 4), c2(2, 10), c3;
    cout<<"c1="<<c1<<endl;
    cout<<""c2="<<c2<<endl;
    c3=c1-c2;
    cout<<"c3=c1-c2"<<c3<<endl;
    c3=c1+c2;
    cout<<"c3=c1+c2"<<c3<<endl;
    return 0;
}
为什么运行时会出现错误:fatal error C1001: INTERNAL COMPILER ERROR
怎么回事?
搜索更多相关主题的帖子: double public include private Complex 
2013-06-06 16:50
zhuxiaoneng
Rank: 4
等 级:业余侠客
威 望:2
帖 子:51
专家分:215
注 册:2013-4-10
收藏
得分:0 
cout<<""c2="<<c2<<endl;

改为
cout<<"c2="<<c2<<endl;

多了个引号
2013-06-06 17:00
chen1204019
Rank: 1
来 自:广东
等 级:新手上路
帖 子:93
专家分:0
注 册:2012-12-3
收藏
得分:0 
少了也还是错啊

新手发言,请多指教!
2013-06-06 18:06
haoyasen
Rank: 2
等 级:论坛游民
帖 子:90
专家分:20
注 册:2013-3-30
收藏
得分:0 
我这 没报错阿紫
2013-06-06 22:37
雪狼63381
Rank: 2
来 自:河南
等 级:论坛游民
帖 子:22
专家分:51
注 册:2013-5-31
收藏
得分:0 
重载时,把引用前面的const去掉,因为const表常量
2013-06-06 22:55
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
INTERNAL COMPILER ERROR 说的是 编译器出错了,而不是 编译出错了

另外,顺便问一句,例如 friend ostream &operator<<(ostream &out, const Complex &c); 中
为什么将类型 ostream& 要生生的拆成两半,变成了 ostream & ?
为什么将毫无关系的 & 和 out 要生生的绑在一起,变成了 &out?
2013-06-07 08:39
快速回复:运算符重载为友元函数的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015422 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved