求解答!类的友元函数+复数类中的运算符重载 +类模版出问题

有人跟我一样懒 2014-04-16 11:47:06
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 作 者:王颖
* 完成日期:2014 年 4 月 14 日
* 版 本 号:v1.0
* 输入描述: 无
* 问题描述:类的友元函数+复数类中的运算符重载+类模版
* 请用类的成员函数,定义复数类重载运算符+、-、*、/,使之能用于复数的加减乘除
* 程序输出:略
* 问题分析:略
* 算法设计:略
*/
#include <iostream>

using namespace std;
template <class numtype>
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(numtype r,numtype i){real=r; imag=i;}
friend Complex<numtype> operator+(Complex<numtype>&,Complex<numtype>&);
friend Complex<numtype> operator-(Complex<numtype>&,Complex<numtype>&);
friend Complex<numtype> operator*(Complex<numtype>&,Complex<numtype>&);
friend Complex<numtype> operator/(Complex<numtype>&,Complex<numtype>&);
void display()
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}
private:
numtype real;
numtype imag;
};
//下面定义成员函数
template <class numtype>
Complex<numtype> operator+(Complex<numtype> &c2,Complex<numtype>&t)
{
Complex<numtype> mm;
mm.imag=t.real+c2.real;
mm.real=t.imag+c2.imag;
return mm;
}
template <class numtype>
Complex<numtype> operator-(Complex<numtype> &c2,Complex<numtype>&t)
{
return Complex<numtype>(c2.real-t.real,c2.imag-t.imag);
}
template <class numtype>
Complex<numtype> operator*(Complex<numtype> &c2,Complex<numtype> &t)
{
return Complex<numtype>(t.real*c2.real,t.imag*c2.imag);
}
template <class numtype>
Complex<numtype> operator/(Complex<numtype> &c2,Complex<numtype> &t)
{
return Complex<numtype>(c2.real/t.real,c2.imag/t.imag);
}
//下面定义用于测试的main()函数
int main()
{
Complex<int> c1(3,4),c2(5,-10),c3(0,0);
Complex<double> c4(2.5,6.6),c5(5.5,9.9),c6;
cout<<"c1=";

c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
cout<<"c4=";
c3.display();
cout<<"c5=";
c3.display();
c6=c4+c5;
cout<<"c4+c5=";
c3.display();
c6=c4-c5;
cout<<"c4-c5=";
c3.display();
c6=c4*c5;
cout<<"c4*c5=";
c3.display();
c6=c4/c5;
cout<<"c4/c5=";
c3.display();
return 0;
}


想用:类的友元函数+复数类中的运算符重载+类模版。用类的友元函数+复数类中的运算符重载 的时候还没有错。用了模版就错了一堆。找不到原因。。。求解答啊,谢谢各位大神呀!
...全文
280 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 7 楼 ri_aje 的回复:
[quote=引用 5 楼 u012451600 的回复:] [quote=引用 3 楼 ri_aje 的回复:] [quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
引用 3 楼 ri_aje 的回复:
[quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
引用 3 楼 ri_aje 的回复:
[quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 作    者:王颖
* 完成日期:2014 年 4 月 14 日
* 版 本 号:v1.0
* 输入描述: 无
* 问题描述:类的友元函数+复数类中的运算符重载+类模版
* 请用类的成员函数,定义复数类重载运算符+、-、*、/,使之能用于复数的加减乘除
* 程序输出:略
* 问题分析:略
* 算法设计:略
*/
#include <iostream>

using namespace std;
template <class numtype>
class Complex
{
public:
    Complex()
    {
        real=0;
        imag=0;
    }
    Complex(numtype r,numtype i)
    {
        real=r;
        imag=i;
    }
    friend Complex<numtype> operator+(Complex<numtype>&c2,Complex<numtype>&t)
    {
        Complex<numtype> mm;
        mm.imag=t.real+c2.real;
        mm.real=t.imag+c2.imag;
        return mm;
    }
    friend Complex<numtype> operator-(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(c2.real-real,c2.imag-imag);
    }
    friend Complex<numtype> operator*(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(t.real*c2.real,t.imag*c2.imag);
    }
    friend Complex<numtype> operator/(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(c2.real/t.real,c2.imag/t.imag);
    }
    void display()
    {
        cout<<"("<<real<<","<<imag<<")"<<endl;
    }
private:
    numtype real;
    numtype imag;
};
//下面定义用于测试的main()函数
int main()
{
    Complex<int> c1(3,4),c2(5,-10),c3(0,0);
    Complex<double> c4(2.5,6.6),c5(5.5,9.9),c6;
    cout<<"c1=";

    c1.display();
    cout<<"c2=";
    c2.display();
    c3=c1+c2;
    cout<<"c1+c2=";
    c3.display();
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.display();
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.display();
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.display();
    cout<<"c4=";
    c3.display();
    cout<<"c5=";
    c3.display();
    c6=c4+c5;
    cout<<"c4+c5=";
    c3.display();
    c6=c4-c5;
    cout<<"c4-c5=";
    c3.display();
    c6=c4*c5;
    cout<<"c4*c5=";
    c3.display();
    c6=c4/c5;
    cout<<"c4/c5=";
    c3.display();
    return 0;
}
[/quote] 试了一下,这句

        return Complex<numtype>(c2.real-real,c2.imag-imag);
改成这样就好了。

        return Complex<numtype>(c2.real-t.real,c2.imag-t.imag);
[/quote]最近一直忙的没时间上网,刚看到回复,我按照您的改了改,没有错误和警告了。很谢谢您!
果冻虾仁 2014-04-21
  • 打赏
  • 举报
回复
额,我感觉+ - * / 运算符是没必要用友元的。比如两个复数对象 a和b。a+b 实际上就是在调用一个成员函数。不使用友元直接重载加号运算符
Complex<numtype>& operator+(Complex<numtype>&,Complex<numtype>&);
注意返回值那里加了引用。不需要友元。a+b。实际就是可以理解为调用 a.operator+ 这个方法而已。 只有跟输入输出有关的<< 和>>需要使用友元 。因为不是复数对象在调用,而是输出流对象在调用。比如你重载<< cout<<a那么就是在执行cout.operator<<方法
mujiok2003 2014-04-21
  • 打赏
  • 举报
回复
ri_aje 2014-04-20
  • 打赏
  • 举报
回复
引用 5 楼 u012451600 的回复:
[quote=引用 3 楼 ri_aje 的回复:] [quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
引用 3 楼 ri_aje 的回复:
[quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
引用 3 楼 ri_aje 的回复:
[quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 作    者:王颖
* 完成日期:2014 年 4 月 14 日
* 版 本 号:v1.0
* 输入描述: 无
* 问题描述:类的友元函数+复数类中的运算符重载+类模版
* 请用类的成员函数,定义复数类重载运算符+、-、*、/,使之能用于复数的加减乘除
* 程序输出:略
* 问题分析:略
* 算法设计:略
*/
#include <iostream>

using namespace std;
template <class numtype>
class Complex
{
public:
    Complex()
    {
        real=0;
        imag=0;
    }
    Complex(numtype r,numtype i)
    {
        real=r;
        imag=i;
    }
    friend Complex<numtype> operator+(Complex<numtype>&c2,Complex<numtype>&t)
    {
        Complex<numtype> mm;
        mm.imag=t.real+c2.real;
        mm.real=t.imag+c2.imag;
        return mm;
    }
    friend Complex<numtype> operator-(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(c2.real-real,c2.imag-imag);
    }
    friend Complex<numtype> operator*(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(t.real*c2.real,t.imag*c2.imag);
    }
    friend Complex<numtype> operator/(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(c2.real/t.real,c2.imag/t.imag);
    }
    void display()
    {
        cout<<"("<<real<<","<<imag<<")"<<endl;
    }
private:
    numtype real;
    numtype imag;
};
//下面定义用于测试的main()函数
int main()
{
    Complex<int> c1(3,4),c2(5,-10),c3(0,0);
    Complex<double> c4(2.5,6.6),c5(5.5,9.9),c6;
    cout<<"c1=";

    c1.display();
    cout<<"c2=";
    c2.display();
    c3=c1+c2;
    cout<<"c1+c2=";
    c3.display();
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.display();
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.display();
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.display();
    cout<<"c4=";
    c3.display();
    cout<<"c5=";
    c3.display();
    c6=c4+c5;
    cout<<"c4+c5=";
    c3.display();
    c6=c4-c5;
    cout<<"c4-c5=";
    c3.display();
    c6=c4*c5;
    cout<<"c4*c5=";
    c3.display();
    c6=c4/c5;
    cout<<"c4/c5=";
    c3.display();
    return 0;
}
[/quote] 试了一下,这句

        return Complex<numtype>(c2.real-real,c2.imag-imag);
改成这样就好了。

        return Complex<numtype>(c2.real-t.real,c2.imag-t.imag);
ri_aje 2014-04-19
  • 打赏
  • 举报
回复
引用 2 楼 u012451600 的回复:
[quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。
火头军 2014-04-19
  • 打赏
  • 举报
回复
模板类的运算符重载不要用类内friend,类外实现,因为类外会和全局模板函数冲突,编译器会认为你的friend没有实现。最好用成员函数
  • 打赏
  • 举报
回复
引用 4 楼 liu111qiang88 的回复:
模板类的运算符重载不要用类内friend,类外实现,因为类外会和全局模板函数冲突,编译器会认为你的friend没有实现。最好用成员函数
哦哦,这样啊。谢谢了
  • 打赏
  • 举报
回复
引用 3 楼 ri_aje 的回复:
[quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
引用 3 楼 ri_aje 的回复:
[quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
引用 3 楼 ri_aje 的回复:
[quote=引用 2 楼 u012451600 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?[/quote] 把新代码上来。[/quote]
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 作    者:王颖
* 完成日期:2014 年 4 月 14 日
* 版 本 号:v1.0
* 输入描述: 无
* 问题描述:类的友元函数+复数类中的运算符重载+类模版
* 请用类的成员函数,定义复数类重载运算符+、-、*、/,使之能用于复数的加减乘除
* 程序输出:略
* 问题分析:略
* 算法设计:略
*/
#include <iostream>

using namespace std;
template <class numtype>
class Complex
{
public:
    Complex()
    {
        real=0;
        imag=0;
    }
    Complex(numtype r,numtype i)
    {
        real=r;
        imag=i;
    }
    friend Complex<numtype> operator+(Complex<numtype>&c2,Complex<numtype>&t)
    {
        Complex<numtype> mm;
        mm.imag=t.real+c2.real;
        mm.real=t.imag+c2.imag;
        return mm;
    }
    friend Complex<numtype> operator-(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(c2.real-real,c2.imag-imag);
    }
    friend Complex<numtype> operator*(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(t.real*c2.real,t.imag*c2.imag);
    }
    friend Complex<numtype> operator/(Complex<numtype>&c2,Complex<numtype>&t)
    {
        return Complex<numtype>(c2.real/t.real,c2.imag/t.imag);
    }
    void display()
    {
        cout<<"("<<real<<","<<imag<<")"<<endl;
    }
private:
    numtype real;
    numtype imag;
};
//下面定义用于测试的main()函数
int main()
{
    Complex<int> c1(3,4),c2(5,-10),c3(0,0);
    Complex<double> c4(2.5,6.6),c5(5.5,9.9),c6;
    cout<<"c1=";

    c1.display();
    cout<<"c2=";
    c2.display();
    c3=c1+c2;
    cout<<"c1+c2=";
    c3.display();
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.display();
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.display();
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.display();
    cout<<"c4=";
    c3.display();
    cout<<"c5=";
    c3.display();
    c6=c4+c5;
    cout<<"c4+c5=";
    c3.display();
    c6=c4-c5;
    cout<<"c4-c5=";
    c3.display();
    c6=c4*c5;
    cout<<"c4*c5=";
    c3.display();
    c6=c4/c5;
    cout<<"c4/c5=";
    c3.display();
    return 0;
}
  • 打赏
  • 举报
回复
引用 1 楼 ri_aje 的回复:
把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。
我把friend的定义直接写在内部了。但是还是很多错误啊呀?是不是还有其他的限制?
ri_aje 2014-04-17
  • 打赏
  • 举报
回复
把 friend 的定义直接写在类内部就行了。否则就得先声明函数模板,然后再 friend 特化,很麻烦。

65,187

社区成员

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

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