标题:友元重载
取消只看楼主
tfg0116
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2008-10-9
 问题点数:0 回复次数: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 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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-243882-1-1.html




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

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