- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一辈子都弄不明白为什么会出现这些错误,尤其是因为我已经包含了 guard 。
这些是我的错误(请忽略我给电脑起的名字):
1>main.obj : error LNK2005: "class std::basic_ostream > >& __cdecl operator<<(class std::basic_ostream > &,class >Date &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVDate@@@Z) already >defined in loan.obj
1>main.obj : error LNK2005: "class std::basic_ostream > >& __cdecl operator<<(class std::basic_ostream > &,class >Loan &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVLoan@@@Z) already >defined in loan.obj
1>C:\Users\SweetAssSarah\Documents\Visual Studio >2012\Projects\ConsoleApplication4\Debug\a1.exe : fatal error LNK1169: one or more multiply >defined symbols found
这是我的 4 个文件: main.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 <<"\n\n" << "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 文件中包含 header ,则会出现编译器错误,因为相同的代码将被编译成多个 .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;
}
通过将函数声明为静态的,它具有内部链接。您仍将决定编译器是否将其内联到编译器。
关于c++ - 已经定义了 Obj?链接错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19148639/
一段时间以来,我一直在做这个反复出现的噩梦(阅读 - 我的应用程序中的错误)。出于某种原因,某个计时器在我停止后继续发送“Elapsed”事件,即使 在事件本身 计时器“承认”已被禁用!检查一下: /
为了找到 2 个 git 分支的共同祖先,需要做的是: git merge-base branch another_branch 好的。但是……如果两个分支都已经 merge 了怎么办?当我在这种情况
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我想要一个相机 View ,可以将图像捕获到本地文件或让用户从本地照片库中选择图像。我想也许有人为此编写了很好的库/代码。也许我可以利用它。已经有好的了吗?谢谢。我只是避免重新发明轮子:) 最佳答案
我从 master 分支创建了一个功能分支。之后有来自功能分支的提交 [F1]。 [F1] -- Feature Branch / [M1]-[M2
我喜欢使用 .NET 进行编程,尤其是 C# 3.0、.NET 3.5 和 WPF。但我特别喜欢的是 Mono .NET 确实与平台无关。 现在我听说了 Mono 中的 Olive 项目。我找不到某种
介绍和搜索 所以我认为我犯了一个严重的错误,我很担心。我已经分析了独立负责人的论坛,我已经接近找到答案,但场景太具体,不适用于我所在的位置。如果您找到可以回答我的问题的特定主题,请链接我。 例如:Ho
我有一个类似于下图的提交图。标记为 * 的提交表示大量提交。 A* | B--------- | | C* D* master 和 cor
我喜欢使用 .NET 进行编程,尤其是 C# 3.0、.NET 3.5 和 WPF。但我特别喜欢的是 Mono .NET 确实与平台无关。 现在我听说了 Mono 中的 Olive 项目。我找不到某种
我们最近接手了一个 .NET 项目,在查看 db 后,我们在某些列中有以下内容: 1)某些列具有诸如" & etc etc 2) 有些有 标签和其他非 html 编码的标签 这些数据
你好,当我导航到应用程序中的另一个页面时出现此错误 我不知道为什么这个错误出现 #0 _AsyncCompleter.complete (dart:async/future_impl.da
我使用以下 C 算法计算数据的 CRC32: #define CRC32_POLYNOM_REVERSED 0xEDB88320 uint32 calcCrc32(uint8* buffer, u
我试图在我的一个测试中断言模型中的字段没有改变。我知道从哲学上这是不正确的,但由于我控制了我需要知道的所有变量,所以我只想检查我的数据库条目是否没有改变。 我愿意接受一个解决方案,该解决方案可以将其转
我是 GitHub 的新手。并通过 Eclipse 使用它我们是两个人在开发一个应用程序。当我在 Git shell 中检查 git status 时,我得到以下状态。 On branch maste
简单代码: std::ifstream file("file.txt"); std::string line; while(getline(file,line)) ; //exhaust file
是的,我又找不到这个 Gradle DSL 方法:'compile()' 问题。 我检查了我有: buildscript { repositories { jcenter()
HTML: articles CSS: #main_menu { float: left; padding-top: 10px; vertical-align: m
我是一名优秀的程序员,十分优秀!