2013-08-28 63 views
0

好吧,所以我幾個月沒有使用C++,而我的一個問題一直使用多個頭文件。目前我的問題是我的所有類的頭文件都鏈接到.cpp文件使用的主頭文件。我使用IFNDEF是,以確保沒有被重複,但我認爲當一個文件組得到已經編譯由於我的體型輸出是每個.cpp創建兩個LNK2005錯誤

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)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@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)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@AAV01[email protected]@@@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 

我一直在努力嘗試了一個多小時來解決這個問題,任何有識之士將不勝感激

編輯:

重載< <

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; 

}; 

地址是結構

+2

大概你在頭文件中定義了一個'ostream&operator <<(ostream&os,...)'函數,而不是在.cpp文件中定義它(或者,你可以聲明它是'inline',但是大多數打印操作都足夠複雜,因此不需要內聯更有意義) –

+1

請使用namespace std;來自所有標題。 – drescherjm

+0

謝謝,我已經重載了<<我的結構在它們被定義的標題中,但是當我嘗試將它移動到一個通用函數cpp文件時,我得到了 錯誤C2679:binary'<<':no operator發現它需要一個類型爲'Name'的右側操作數(或者沒有可接受的轉換) – Fluzzarn

回答

1

將函數定義放在.cpp文件中,就像註釋中所說的那樣。爲了防止「沒有運營商發現」誤差必須保持在函數聲明中headerfile:

std::ostream& operator<<(std::ostream& os,const Address ad); 

不要在該行的末尾忘了分號。並且請注意,該聲明只包含函數標題,並且沒有正文。

而你應該通過ad作爲參考,但這只是一個小細節,並且與你的問題無關。