鱼C论坛

 找回密码
 立即注册
查看: 1991|回复: 2

[已解决](C++)为什么这里的类模板友元函数会报错显示:无法解析的外部符号

[复制链接]
发表于 2019-5-16 16:15:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下:
  1.                 #include<iostream>
  2. using namespace std;
  3. template <class Value>
  4. class Complex
  5. {
  6. private:
  7.         Value real;
  8.         Value image;
  9. public:
  10.         Complex(Value xx = 0, Value yy = 0);
  11.         template<class T>
  12.         friend Complex operator + (const Complex<T> &a, const Complex<T> &b);
  13.         template<class T>
  14.         friend Complex operator - (const Complex<T> &a, const Complex<T> &b);
  15.         void Print();
  16. };

  17. int main()
  18. {
  19.         double a, b, c, d;
  20.         cout << "请输入第一个复数的实部虚部:" << endl;
  21.         cout << "虚部:";
  22.         cin >> a;
  23.         cout << "实部:";
  24.         cin >> b;
  25.         cout << "请输入第二个复数的实部虚部:" << endl;
  26.         cout << "虚部:";
  27.         cin >> c;
  28.         cout << "实部:";
  29.         cin >> d;
  30.         Complex<double> aa(a, b), bb(c, d),cc;
  31.         aa + bb;
  32.         cout << "两复数相加的值为:";
  33.         cc.Print();
  34.         cout << "两复数相减的值为:";
  35.         aa - bb;
  36.         cc.Print();
  37.         system("Pause");
  38.         return 0;
  39. }

  40. template <class Value>
  41. Complex<Value>::Complex(Value cc, Value dd)
  42. {
  43.         real = cc;
  44.         image = dd;
  45. }

  46. template<class Value>
  47. void Complex<Value>::Print()
  48. {
  49.         cout << real;
  50.         if (image != 0)
  51.         {
  52.                 if (image > 0)
  53.                         cout << " + ";
  54.                 cout << image << "i ";
  55.         }
  56.         cout << endl;
  57. }

  58. template<class T>
  59. Complex<T> operator + (Complex<T> &a, Complex<T> &b)
  60. {
  61.         Complex<T> copy;
  62.         copy.real = a.real + b.real;
  63.         copy.image = a.image + b.image;
  64.         return copy;
  65. }

  66. template<class T>
  67. Complex<T> operator - (Complex<T> &a, Complex<T> &b)
  68. {
  69.         Complex<T> copy;
  70.         copy.real = a.real - b.real;
  71.         copy.image = a.image - b.image;
  72.         return copy;
  73. }
复制代码
最佳答案
2019-5-16 17:57:29
本帖最后由 Croper 于 2019-5-16 18:00 编辑

首先,加不加const是两个函数,你的友元声明里有const,后面的定义里却没有const,这是第一个问题

第二,友元声明里你的返回值没有模板参数,按照定义,会自动套用原来的模板参数,于是,你的友元声明其实是这样的:
  1. template <typename Value>
  2. template <typename T>
  3. Complex<Value> operator+(const Complex<T>&,const Complex<T>&);
复制代码

然而,C++并不允许重载仅仅只有返回值类型不同的函数。因此,你这个友元声明是错误的。

改正以上两点已经正确可以运行了


这里提一下,你这样重载的话其实建立的并不是一一对应的友元关系,而是把所有类型的operator+都声明为了友元,换句话说,operator+<int>也可以访问Complex<double>的私有数据成员。
所以如果追求一一对应的话,那么需要把函数声明,友元声明和函数定义分开写。


一对多的友元声明我就不给代码了,你把前两点改了就行,
这是一一对应的友元声明的代码:
  1. #include<iostream>
  2. using namespace std;

  3. template <typename T> class Complex;  //首先声明类

  4. template <typename T> Complex<T> operator+(const Complex<T>&, const Complex<T>&);  //函数声明
  5. template <typename T> Complex<T> operator-(const Complex<T>&, const Complex<T>&);


  6. template <class T>
  7. class Complex
  8. {
  9.         friend Complex operator+<T>(const Complex&, const Complex&);  //友元声明
  10.         friend Complex operator-<T>(const Complex&, const Complex&);
  11.        
  12.         ...
  13. };
  14. ...

  15. template<class T>
  16. Complex<T> operator + (const Complex<T> &a, const  Complex<T> &b)  //函数定义
  17. {
  18.         Complex<T> copy;
  19.         copy.real = a.real + b.real;
  20.         copy.image = a.image + b.image;
  21.         return copy;
  22. }

  23. template<class T>
  24. Complex<T> operator - (const Complex<T> &a, const  Complex<T> &b)
  25. {
  26.         Complex<T> copy;
  27.         copy.real = a.real - b.real;
  28.         copy.image = a.image - b.image;
  29.         return copy;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-16 17:57:29 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Croper 于 2019-5-16 18:00 编辑

首先,加不加const是两个函数,你的友元声明里有const,后面的定义里却没有const,这是第一个问题

第二,友元声明里你的返回值没有模板参数,按照定义,会自动套用原来的模板参数,于是,你的友元声明其实是这样的:
  1. template <typename Value>
  2. template <typename T>
  3. Complex<Value> operator+(const Complex<T>&,const Complex<T>&);
复制代码

然而,C++并不允许重载仅仅只有返回值类型不同的函数。因此,你这个友元声明是错误的。

改正以上两点已经正确可以运行了


这里提一下,你这样重载的话其实建立的并不是一一对应的友元关系,而是把所有类型的operator+都声明为了友元,换句话说,operator+<int>也可以访问Complex<double>的私有数据成员。
所以如果追求一一对应的话,那么需要把函数声明,友元声明和函数定义分开写。


一对多的友元声明我就不给代码了,你把前两点改了就行,
这是一一对应的友元声明的代码:
  1. #include<iostream>
  2. using namespace std;

  3. template <typename T> class Complex;  //首先声明类

  4. template <typename T> Complex<T> operator+(const Complex<T>&, const Complex<T>&);  //函数声明
  5. template <typename T> Complex<T> operator-(const Complex<T>&, const Complex<T>&);


  6. template <class T>
  7. class Complex
  8. {
  9.         friend Complex operator+<T>(const Complex&, const Complex&);  //友元声明
  10.         friend Complex operator-<T>(const Complex&, const Complex&);
  11.        
  12.         ...
  13. };
  14. ...

  15. template<class T>
  16. Complex<T> operator + (const Complex<T> &a, const  Complex<T> &b)  //函数定义
  17. {
  18.         Complex<T> copy;
  19.         copy.real = a.real + b.real;
  20.         copy.image = a.image + b.image;
  21.         return copy;
  22. }

  23. template<class T>
  24. Complex<T> operator - (const Complex<T> &a, const  Complex<T> &b)
  25. {
  26.         Complex<T> copy;
  27.         copy.real = a.real - b.real;
  28.         copy.image = a.image - b.image;
  29.         return copy;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-16 18:26:29 | 显示全部楼层
Croper 发表于 2019-5-16 17:57
首先,加不加const是两个函数,你的友元声明里有const,后面的定义里却没有const,这是第一个问题

第二 ...

懂了,谢谢。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 07:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表