好的,所以我已经有几个月没有使用 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”,它本身包括所有其他头文件。
页眉.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
我一直在努力解决这个问题一个多小时,任何见解都将不胜感激
编辑:
重载<<
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;
};
地址是一个结构