好的,所以我几个月没有使用过C ++,而我的一个问题一直是使用多个标题。目前我的问题是所有类的标题都链接到.cpp文件使用的主标题。我正在使用ifndef来确保没有重复,但我认为问题是当一组文件由于我的构建输出而被编译时
1> student.cpp
1> person.cpp
1> main.cpp
1> functions.cpp
1> faculty.cpp
1> Generating Code...
1>functions.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Address)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UAddress@@@Z) already defined in faculty.obj
1>functions.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Name)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UName@@@Z) already defined in faculty.obj
1>main.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Address)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UAddress@@@Z) already defined in faculty.obj
1>main.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Name)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UName@@@Z) already defined in faculty.obj
1>person.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Address)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UAddress@@@Z) already defined in faculty.obj
1>person.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Name)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UName@@@Z) already defined in faculty.obj
1>student.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Address)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UAddress@@@Z) already defined in faculty.obj
1>student.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct Name)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@UName@@@Z) already defined in faculty.obj
1>C:\Users\Fluzzarn\Documents\Visual Studio 2012\Projects\pa1\Debug\pa1.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.24
所有cpp文件只包含我的“header.h”,它本身包含所有其他标题。
Header.h:
#ifndef HEADER_H
#define HEADER_H
#include "person.h"
#include "faculty.h"
#include "student.h"
#include <iostream>
#include <fstream>
#include <list>
#include <sstream>
using namespace std;
bool searchForUser();
void loadFromFile(std::string fileName, std::list<Person> targetList);
void loadBasicInfo(std::fstream& fileReader,Person tempPerson);
#endif
我一直在努力解决这个问题超过一个小时,任何见解都会受到赞赏
编辑:
重载&lt;&lt;
std::ostream& operator<<(std::ostream& os,const Address ad)
{
os << ad.mStreetAddress << std::endl;
os << ad.mCity << " , " << ad.mState << std::endl;
os << ad.mZip;
return os;
};
地址是结构
答案 0 :(得分:1)
您将函数定义放在.cpp文件中,就像评论所说的那样。为了防止“找不到运算符” - 错误,你必须在头文件中保留函数声明:
std::ostream& operator<<(std::ostream& os,const Address ad);
不要忘记行尾的分号。请注意,声明只包含函数头,而且没有正文。
你应该传递ad
作为参考,但这只是一个小细节,与你的问题无关。