2013-02-15 103 views
1

它顯示「鏈接器錯誤:致命錯誤LNK1120:1無法解析的外部」 整個項目=頭文件,第1個.cpp文件和第2個.cpp文件與main()函數。 Any1有想法什麼是做錯了嗎?鏈接器錯誤:致命錯誤LNK1120:1無法解析的外部鏈接

//golf.h for golf.cpp 
const int Len = 40; 
struct golf{ 
    char fullname[Len]; 
    int handicap; 
}; 
//dla gotowych wartosci 
void setgolf (golf & g, const char * name, int hc); 
//do wpisywania wartosci 
int setgolf (golf & g); 
//tylko handicap 
inline void handicap (golf & g, int hc); 
//wyswietla info o graczu 
void showgolf (const golf & g) ; 

下一個文件

//golf.cpp for 9-1.cpp 
#include <iostream> 
#include <cstring> 
#include "golf.h" 
extern const int Len; 
void setgolf (golf & g, const char* name, int hc){ 
    strcpy(g.fullname,name); 
    g.handicap=hc; 
} 
int setgolf (golf & g){ 
    using namespace std; 
    cout << "Podaj fullname golfiarza" << endl; 
    if (cin.getline(g.fullname,Len)) return 0; 
    cout << "Teraz podaj jego handicap" << endl; 
    cin >> g.handicap; 
    return 1; 
} 
inline void handicap (golf & g, int hc){ 
    g.handicap=hc; 
} 
void showgolf (const golf & g){ 
    using std::cout; 
    using std::endl; 
    cout << "Fullname gracza to: " << g.fullname << endl; 
    cout << "Jego handicap to: " << g.handicap << endl; 
} 

LAST FILE

#include <iostream> 
#include <cstdlib> 
#include "golf.h" 
using namespace std; 
int main(){ 
    cout << "Witaj!\nTutaj program golficzny!" << endl; 
    golf filip; 
    golf klaudia; 
    cout << "Automatyczne uzupelnienie Filipa" << endl; 
    setgolf(filip, "Filip Bartuzi",100); 
    showgolf(filip); 
    cout << "Manualne uzupelnienie Klaudii" << endl; 
    ((setgolf(klaudia))==1) ? showgolf(klaudia) : cout << "nie wprowadziles gracza!" << endl; ; 
    cout << "Zly handicap? Okey, zmienie handicap Filipowi" << endl; 
    handicap(filip,50); 
    showgolf(filip); 
    cout << "Od razu lepiej, nieprawda?" << endl; 
    system("PAUSE"); 
    return 0; 
} 

任何想法?

+1

錯誤的下一行是什麼? – 2013-02-15 21:09:00

+0

1>生成代碼... 1> 9-1.obj:error LNK2019:無法解析的外部符號「void __cdecl handicap(struct golf&,int)」(?handicap @@ YAXAAUgolf @@ H @ Z)在函數中引用_main 1> C:\ Users \ Filip \ Dysk Google \ C++ \ Debug \ ConsoleApplication7.exe:致命錯誤LNK1120:1無法解析的外部設備 ========== Build:0成功,1次失敗,0次到目前爲止,0跳過========== – 2013-02-15 21:09:32

回答

4
inline void handicap (golf & g, int hc){ 
    g.handicap=hc; 
} 

嘗試取出inline關鍵字如果函數是在golf.cpp文件。

或者,重新定位golf.h文件中的整個函數。 (在這裏,這個選擇似乎更合適。)

原因:如果函數是inline,那麼它的主體必須對調用者可見。只有將函數體放在頭文件中,而不是實現文件中,纔有可能。

可能相關:C++ inline member function in .cpp file

+0

這工作。你能解釋爲什麼它會破壞調試進程嗎?在線使用有什麼問題? – 2013-02-15 21:12:37

+0

@Filip Bartuzi:不客氣!好問題,我回復了答案。 – Arun 2013-02-15 21:19:08

2

刪除內聯字。它意味着在同一地點提供函數的聲明和定義(這使編譯器可以用代碼替換對函數的每個調用)。

在您的代碼中,您使用inline關鍵字,但在其他地方提供代碼。

它應該是這樣的:

inline void handicap (golf & g, int hc){ g.handicap=hc; }

直接在您的.h文件中。 (並且該定義可以從golf.cpp文件中刪除)

+0

thx bro。它的工作,很好,你解釋它。然後,我可以繼續Stephen Pratta教育的Primer C++。 C呀! – 2013-02-15 21:17:43

相關問題