Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;“未定义的符号”;C+中的错误+;_C++_Templates_Compiler Errors_Operator Overloading - Fatal编程技术网

C++ &引用;“未定义的符号”;C+中的错误+;

C++ &引用;“未定义的符号”;C+中的错误+;,c++,templates,compiler-errors,operator-overloading,C++,Templates,Compiler Errors,Operator Overloading,我有Point类,该类有X、Y和Name作为数据成员。我超载了 T operator-(const Point<T> &); 但问题是这个程序没有编译,我收到了这个错误: Undefined symbols: "Point<double>::operator-(Point<double>&)", referenced from: _main in main.o ld: symbol(s) not found collect2: ld re

我有
Point
类,该类有
X
Y
Name
作为数据成员。我超载了

T operator-(const Point<T> &);
但问题是这个程序没有编译,我收到了这个错误:

Undefined symbols:
"Point<double>::operator-(Point<double>&)", referenced from:
  _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
未定义的符号:
“点::运算符-(点&)”,引用自:
_主音中的主音
ld:找不到符号
collect2:ld返回了1个退出状态

非常感谢您的帮助……

您无法编译非专用模板。您必须将定义代码放在标题中

您需要将点模板类放在.hpp文件中,并在使用点时包含该类。

您必须在使用它们的每个文件中包含模板,否则编译器无法为您的特定类型生成代码

运算符之间还有一个优先级,重载它们时不会更改。您的代码将被视为

(cout << P2) - P1; 

(你能在头文件或.cpp文件中包含运算符的实现吗?@juanchopanza是的。我只有一个.cpp文件,它有实现。高度相关:模板本身不能作为翻译单元的一部分进行编译。你需要实例化或专门化来编译它们。重复:可能重复:我将定义放在.h文件中,仍然收到相同的错误!我将定义放在.h文件中,仍然收到相同的错误!
Undefined symbols:
"Point<double>::operator-(Point<double>&)", referenced from:
  _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
(cout << P2) - P1; 
cout << (P2 - P1);