注册 登录
编程论坛 C++教室

友元重载

tfg0116 发布于 2008-11-12 21:01, 745 次点击
#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)
4 回复
#2
newyj2008-11-12 22:37
程序代码:
#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;
}
#3
cnbj2008-11-12 22:41
头文件改成#include<iostream.h>

 两个return comlpex(c1.real + c2.real, c1.image + c2.image);中的comlpex改成complex
#4
shediao2008-11-13 01:36
兄弟你还有一点没有说吧! 那就是上面的友元函数就不能返回引用吧,
#5
sunkaidong2008-11-13 10:35
两个问题,一个是命名空间问题,一个是局部变量的引用问题..
我下面的程序解决第一个问题,第二个就比较明显了
#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;
}
1