已经定义了 Obj?链接错误
Already defined Obj? Linking error?
我一辈子都无法弄清楚为什么我会出现这些错误,特别是因为我包括警卫。
这些是我的错误(请忽略我为我的计算机命名的内容(:
1>main.obj : 错误 LNK2005: "class std::basic_ostream>>& __cdecl 运算符<<(class std::basic_ostream> &,class>Date &(" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@std@@AAV01@AAVDate@@@Z( 已经>在loan.obj中定义
1>main.obj : 错误 LNK2005: "class std::basic_ostream>>& __cdecl 运算符<<(class std::basic_ostream> &,class>Loan &(" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@std@@AAV01@AAVLoan@@@Z( 已经>在loan.obj中定义
1>C:\Users\SweetAssSarah\Documents\Visual Studio>2012\Projects\ConsoleApplication4\Debug\a1.exe:致命错误 LNK1169:找到一个或多个>定义的符号
这是我的 4 个文件:主.cpp:
#ifndef _main_cpp
#define _main_cpp
#include<iostream>
#include "loan.h"
#include "date.h"
using namespace std;
void main(){
const int MAX_SIZE = 80;
char response[MAX_SIZE];
Loan sarah("Sarah", "123 Awesomeville ", Date (01,February,2010));
cout << sarah.getName() << " address: " << sarah.getAddress() << endl;
cout << "Date: " << sarah.getDate() << endl;
//keep console open until user types a key and enter
cout <<"nn" << "Press ENTER to continue";
cin.getline(response, MAX_SIZE);
return;
}
#endif
贷款.cpp:
#ifndef _loan_cpp
#define _loan_cpp
#include <iostream>
#include "loan.h"
#include "date.h"
using namespace std;
Loan::Loan(char * aName, char * anAddress, Date aDate){
name = aName;
address = anAddress;
date = aDate;
cout <<"CONSTRUCTING: " << name << "n";
}
Loan::~Loan(){
cout << "DESTRUCTING: " << name << endl;
}
char * Loan::getName() {return name;}
char * Loan::getAddress(){return address;}
Date Loan::getDate(){return date;}
void Loan:: printOn(ostream & ostr) {
cout << name << " address: " << address << endl;
}
#endif
贷款.h:
#ifndef _loan_h
#define _loan_h
#include <math.h> //for the pow() function to do exponentiation
#include <iostream>
#include "date.h"
using namespace std;
class Loan{
public:
Loan(char *, char *, Date );//constructor
~Loan();
char * getName();
char * getAddress();
Date getDate();
void printOn(ostream & ostr);
private:
char * name;
char * address;
Date date; //requires class Date to have a default constructor
};
ostream & operator<<(ostream & ostr, Loan & aLoan) {
aLoan.printOn(ostr);
return ostr;
}
#endif
日期.h:
#ifndef _date_h
#define _date_h
#include <iostream>
enum Month {January=1, February, March, April, May, June, July, August,
September, October, November, December};
using namespace std;
class Date{
public:
// Date() {};
Date(int aDay = 1, Month aMonth = May, int aYear = 2005){
day = aDay;
month = aMonth;
year = aYear;
}
void printOn(ostream & o){
o << day << "/" << month << "/" << year;
}
private:
int day;
Month month;
int year;
};
ostream & operator<<(ostream & ostr, Date & d) {
d.printOn(ostr);
return ostr;
}
#endif
请帮忙!
如注释中所述,<<运算符需要在 cpp 文件中内联或定义。在头文件中定义函数时,它将在包含头文件的每个 cpp 文件中编译。如果将标头包含在多个 cpp 文件中,则会出现编译器错误,因为相同的代码将被编译为多个 .obj 文件。链接器不知道要使用哪个 .obj 文件并引发错误。
解决方案 1 - 拆分为 .h 和 .cpp
贷款.h
ostream & operator<<(ostream & ostr, Loan & aLoan);
贷款.cpp
ostream & operator<<(ostream & ostr, Loan & aLoan) {
aLoan.printOn(ostr);
return ostr;
}
解决方案 2 - 内联
贷款.h
inline ostream & operator<<(ostream & ostr, Loan & aLoan) {
aLoan.printOn(ostr);
return ostr;
}
第二种解决方案将导致编译器通过在代码中发生调用的每个位置内联函数代码来解析函数调用。这会导致编译代码中的冗余,对于大型函数,应避免使用冗余。
解决方案 3 - 静态
贷款.h
static ostream & operator<<(ostream & ostr, Loan & aLoan) {
aLoan.printOn(ostr);
return ostr;
}
通过将函数声明为静态,它具有内部链接。您仍然将编译器是否将其内联的决定留给编译器。
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误