| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 743 人关注过本帖, 1 人收藏
标题:友元重载
只看楼主 加入收藏
tfg0116
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2008-10-9
收藏(1)
 问题点数:0 回复次数:4 
友元重载
#include<iostream>

using namespace std;

class complex
{
public:
    complex(double r = 0.0, double i = 0.0)
    {
        real = r;
        image = i;
    }
    friend complex& operator+(complex c1, complex c2);
    friend complex& operator-(complex c1, complex c2);
    void display();
private:
    double real;
    double image;
};
complex& operator+(complex c1, complex c2)
{
    return comlpex(c1.real + c2.real, c1.image + c2.image);
}
complex& operator-(complex c1, complex c2)
{
    return comlpex(c1.real - c2.real, c1.image - c2.image);
}
void complex::display()
{
    cout<<"("<<real<<","<<image<<")\n";
}
int main()
{
    complex c1(5,4), c2(2,10), c3;

    cout<<"c1=";
    c1.display();

    cout<<"c2=";
    c2.display();

    c3 = c1 + c2;
    cout<<"c3=";
    c3.display();

    c3 = c1 - c2;
    cout<<"c3=";
    c3.display();

    return 0;
}
错误提示:Compiling...
Complex1.cpp
D:\vc++\MSDev98\MyProjects\Complex1\Complex1.cpp(13) : 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.

Complex1.obj - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: 重载 
2008-11-12 21:01
newyj
Rank: 2
等 级:新手上路
威 望:3
帖 子:542
专家分:0
注 册:2008-1-4
收藏
得分:0 
程序代码:
#include<iostream>
using namespace std;

class complex
{
public:
    complex(double r = 0.0, double i = 0.0)
    {
        real = r;
        image = i;
    }
    friend complex operator+(complex c1, complex c2);     //不能返回引用
    friend complex operator-(complex c1, complex c2);     //同上
    void display();
private:
    double real;
    double image;
};
complex operator+(complex c1, complex c2)
{
    return complex(c1.real + c2.real, c1.image + c2.image);      //comlpex改为 complex
}
complex operator-(complex c1, complex c2)                        //通上
{
    return complex(c1.real - c2.real, c1.image - c2.image);
}
void complex::display()
{
    cout<<"("<<real<<","<<image<<")\n";
}
int main()
{
    complex c1(5,4), c2(2,10), c3;

    cout<<"c1=";
    c1.display();

    cout<<"c2=";
    c2.display();

    c3 = c1 + c2;
    cout<<"c3=";
    c3.display();

    c3 = c1 - c2;
    cout<<"c3=";
    c3.display();
    return 0;
}
2008-11-12 22:37
cnbj
Rank: 2
等 级:论坛游民
威 望:1
帖 子:67
专家分:55
注 册:2008-11-4
收藏
得分:0 
头文件改成#include<iostream.h>

 两个return comlpex(c1.real + c2.real, c1.image + c2.image);中的comlpex改成complex
2008-11-12 22:41
shediao
Rank: 1
来 自:山东
等 级:新手上路
威 望:1
帖 子:52
专家分:0
注 册:2008-9-23
收藏
得分:0 
兄弟你还有一点没有说吧! 那就是上面的友元函数就不能返回引用吧,
2008-11-13 01:36
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
两个问题,一个是命名空间问题,一个是局部变量的引用问题..
我下面的程序解决第一个问题,第二个就比较明显了
#include<iostream>

using namespace std;
namespace demo{
class complex
{
public:
    complex(double r = 0.0, double i = 0.0)
    {
        real = r;
        image = i;
    }
    friend complex& operator+(complex c1, complex c2);
    friend complex& operator-(complex c1, complex c2);
    void display();
private:
    double real;
    double image;
};
complex& operator+(complex c1, complex c2)
{
    return complex(c1.real + c2.real, c1.image + c2.image);
}
complex& operator-(complex c1, complex c2)
{
    return complex(c1.real - c2.real, c1.image - c2.image);
}
void complex::display()
{
    cout<<"("<<real<<","<<image<<")\n";
}
}
int main()
{
    demo::complex c1(5,4), c2(2,10), c3;

    cout<<"c1=";
    c1.display();

    cout<<"c2=";
    c2.display();

    c3 = c1 + c2;
    cout<<"c3=";
    c3.display();

    c3 = c1 - c2;
    cout<<"c3=";
    c3.display();

    return 0;
}

学习需要安静。。海盗要重新来过。。
2008-11-13 10:35
快速回复:友元重载
数据加载中...
 
   



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

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