2013-06-05 38 views
1

main.cpp中:C++麻煩的extern - 未定義的參考

bool lgstatus; 
User currentUser; 
//... 
int main(){ //... } 

loginwindow.cpp:

void LoginWindow::on_cmdCreate_clicked() 
{ 
    extern bool lgstatus; 
    extern User currentUser; 
    //... 
    currentUser.setMail(ui->txtAccountMail->text().toStdString()); 
    currentUser.setName(ui->txtAccountName->text().toStdString()); 
    currentUser.setPassword(ui->txtAccountPassword->text().toStdString()); 
    //... 
    lgstatus = true; 
} 

我類User有3種功能。他們每個人都將一個字符串作爲參數。我不知道什麼是錯的。如果我更改lgstatus但編碼currenUser,編譯器不會投訴。

類:

class User 
{ 
public: 
    User(); 
    User(const std::string &name, const std::string &password); 
    User(const std::string &name, const std::string &password, const std::string &mail); 

    void setName(const std::string &name); 
    void setMail(const std::string &mail); 
    void setPassword(const std::string &password); 

private: 
    std::string user_name; 
    std::string user_password; 
    std::string user_mail; 
}; 

「設置」功能,只需通過自己的參數傳遞給USER_NAME的。我不認爲這是必要的,以顯示他們。

錯誤:

  • 未定義參照 '用戶:: setMail(的std :: string常量&)'

  • 未定義參照「用戶::的setName(的std :: string常量& ) '

  • 未定義參考`用戶:: setPassword(的std :: string常量&)'

我做錯了什麼?

+2

您是否正在編譯和鏈接'User.cpp'(或哪個文件包含'User'類的實現)以及其他源文件? – 2013-06-05 18:05:43

+1

你有包含文件來顯示'User'類的完整定義嗎? – unxnut

+0

您需要顯示您的用戶類的定義。使用簡單的設置方法,您可能想要定義內聯,並且您可能只有方法聲明。 –

回答

-1

很可能你沒有在你的loginwindow.cpp中做正確的#include。結果,編譯器從來沒有找到正確的函數。