2012-02-02 47 views
0
#ifndef MIGRATINGUSER_H 
#define MIGRATINGUSER_H 
#include <iostream> 
using namespace std; 

class MigratingUser 
{ 
     public: 
     void populateUidAndGuid(string line); 
     private: 
     string uid; 
     string guid; 
}; 
#endif 

,且s頭定義和下面是CPP定義:未定義參照現有類

#include "MigratingUser.h" 
using namespace std; 

void MigratingUser::populateUidAndGuid(string line) 
{ 
    //get the uid and guid 
} 

,這是我正的誤差。我怎麼能解決此問題:

undefined reference to MigratingUser::populateUidAndGuid(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 

的完整性,這是我如何使用類:

MigratingUser muser; 

muser.populateUidAndGuid(temp); 
+0

你有沒有'#include '?在頭文件中使用namespace std;' – 2012-02-02 02:51:24

+1

如果你的代碼被拆分成多個cpp文件,你是否正確鏈接了目標文件?你用什麼命令編譯? – stefanB 2012-02-02 02:51:52

+0

不,我沒有包括由於命名空間標準的字符串。什麼是好的做法?的std :: string? – DarthVader 2012-02-02 02:52:35

回答

1

首先,我真懷疑這就是你得到的錯誤,因爲它的格式不正確。也許你得到了一些關於std::basic_string<char, std::char_traits<char>, std::allocator<char> >的討論,但是編譯器不太可能會吐出你發佈的內容,除非你有一些......有趣的代碼。在將來,複製並粘貼,確切地說是,因爲專家可以從您認爲不重要的細節中獲得驚人的大量信息。如果編輯不正確,可能會嚴重誤導StackOverflow用戶,使他們難以幫助您。現在

,你的問題的最可能的原因是,你是不知道編譯步驟和鏈接一步之間的差異。該「未定義參考」錯誤是鏈接器錯誤。

這意味着你的頭都沒事 - 你的代碼編譯,但您沒有正確把所有的目標文件鏈接在一起末 - 這樣的g+ -c MigratingUser.cpp輸出是不包含的g++ -c main.cpp輸出(或任何您將其命名爲「使用」代碼)。

你可以一氣呵成編譯和鏈接(簡單項目),像這樣:

g++ -o my_executable MigratingUser.cpp main.cpp 

這將編譯和鏈接兩個文件一起

+0

我錯過了make文件中的cpp。謝謝:) – DarthVader 2012-02-02 02:58:54