操作员过载未解决的外部错误LNK1120、LNK2019
Overloading of Operators Unresolved External Error LNK1120,LNK2019
我一直在尝试使一类复数工作,最近才成功地使我的两个朋友函数定义编译无误现在,我试图通过在(这是我的main.cpp文件)ExtraCredit.cpp文件中创建3个复杂的类对象来测试重载运算符
complex x(3.2);
complex y(3.4, 4.1);
complex z;
z = x + y;
cout << z;
程序正确运行到z = x + y
,但当我添加时
cout << z;
输出应为6.2 + 4.1bi
但相反,我得到了这2个错误
LNK1120 1 unresolved externals
LNK2019 unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> >&_cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char>>&,class complex)"(??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@Vcomplex@@@Z)referenced n function_main
在我的ExtraCredit.cpp文件中,我有
#include "stdafx.h" // This is VSO
#include <iostream>
#include "complex.h"
using namespace std;
在复合体顶部.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
在复合体的末端。h
#endif // COMPLEX_H
在我的complex.cpp文件的顶部,我有
#include "stdafx.h"
#include "complex.h"
#include <iostream>
using namespace std;
这是<lt;复杂.h 中的操作员
friend std::ostream &operator<<(std::ostream &out, complex c);
这是complex.cpp 中的实现/定义
std::ostream& operator<< (std::ostream& out, complex const& c) {
return out << c.getReal() << "+" << c.getImag() << "i";
}
您的声明与您的定义不同:
friend std::ostream &operator<<(std::ostream &out, complex c);
std::ostream& operator<< (std::ostream& out, complex const& c) {
return out << c.getReal() << "+" << c.getImag() << "i";
}
您需要将申报单更改为
friend std::ostream &operator<<(std::ostream &out, const complex &c);
相关文章:
- C++、错误LNK1120和LNK2001中未解析的外部符号
- Visual Studio 2010 - C++ 致命错误 LNK1120: 1 个未解析的外部
- 互联网引起链接器错误lnk1120和lnk2001
- 错误LNK1120:未解决的外部
- C++ 错误 LNK1120:在静态函数内部调用时有 1 个未解析的外部
- c++中的静态构造函数和致命错误LNK1120:1个未解析的外部
- SDL错误LNK1120:1未解决的外部
- QT错误:LNK1120:1未解决的外部Main.OBJ:-1:错误:LNK2019运行QMAKE
- C++使用 Visual Studio 编译错误(LNK1120 和 LNK2019)
- SDL 错误LNK1120 & LNK2019
- 如何使用SFML修复C++游戏中的致命错误LNK1120
- 编译任何内容时出现奇怪的错误,VS 2010:致命错误LNK1120:1个未解决的外部
- 操作员过载未解决的外部错误LNK1120、LNK2019
- C++致命错误LNK1120
- 致命错误LNK1120:1 个未解析的外部 & 错误 LNK2019:函数___tmainCRTStartup中引用_main未解析的外部符号
- Ogre 3D错误-LNK1120:1个未解析的外部
- 致命错误LNK1120:8个未解析的外部
- VCExpress中的C++程序存在致命错误LNK1120
- LNK2019:函数"函数"中'symbol'引用未解析的外部符号;致命错误 LNK1120:1 未解析的外部
- 程序应该运行…致命错误LNK1120和错误LNK2019