2012-10-23 23 views
-1

有一個錯誤,當我嘗試編譯和即時消息不知道它有什麼問題。 這是一個使用文本文件來驗證用戶名和密碼的程序,用「;」分隔一個文本文件中的分隔符。 錯誤很長。有身份驗證問題,如果我使用if語句

 
/tmp/ccgs7RYV.o: In function 'Employee::Employee()': 
main2.cpp:(.text+0xa5): undefined reference to 'Employee::authenticate(std::basic_string, std::allocator>, std::basic_string, std::allocator>)' 
/tmp/ccgs7RYV.o: In function `Employee::Employee()': 
main2.cpp:(.text+0x231): undefined reference to 'Employee::authenticate(std::basic_string, std::allocator>, std::basic_string, std::allocator>)' 
collect2: ld returned 1 exit status 
#include<iostream> 
#include<string> 
#include <fstream> 

using namespace std; 

class Employee 
{ 
public: 
Employee(); 
bool authenticate(string, string); 
}; 

Employee::Employee() 
{ 
    string username, password; 
    cout << "Username: "; 
    cin >> username; 

    cout << "Password: "; 
    cin >> password; 

    if (authenticate(username, password) == true) 
     cout << "Sucess" << endl; 
    else 
     cout << "fail" << endl; 
} 

bool authenticate(string username, string password) 
{ 
    std::ifstream file("login.txt"); 
    std::string fusername, fpassword; 

    while (!file.fail()) 
    { 
     std::getline(file, fusername, ';'); // use ; as delimiter 
     std::getline(file, fpassword); // use line end as delimiter 
     // remember - delimiter readed from input but not added to output 

     if (fusername == username && fpassword == password) 
      return true; 
    } 

    return false; 
} 


int main() 
{ 
    Employee(); 
    return 0; 
} 
+0

請1)停止使用[功課]標籤,它說在說明停止使用2)開始與你在編寫編程語言標記您的問題。 – BoltClock

+0

你while循環已基本破壞。你不需要'fail()',但是你必須檢查'getline'的返回值。 –

+0

類並不是真的被設計爲像構造函數中的所有邏輯一樣被作爲臨時對象調用。 爲什麼不把Employee作爲一個對象,然後給它一個方法進行驗證,該方法需要輸入文件? – CashCow

回答

4
bool Employee::authenticate(string username, string password) { 
std::ifstream file("login.txt"); 
std::string fusername, fpassword; 

while (!file.fail()) { 
    std::getline(file, fusername, ';'); // use ; as delimiter 
    std::getline(file, fpassword); // use line end as delimiter 
    // remember - delimiter readed from input but not added to output 
    if (fusername == username && fpassword == password) 
     return true; 
} 

您需要使用範圍解析操作。你只是想念那個。

+0

omg不能想象它的一個愚蠢的錯誤:(,謝謝指出它!) –

+0

它發生在我們身上。記得接受:P(以及您的其他問題) –

1

好吧,我將嘗試梳理一下課程設計。

class Employee 
{ 
public: 
     Employee(std::string name, std::string password) : 
      m_name(name), m_password(password) 
     { 
     } 

     bool authenticate(const char * filename) const; 

private: 
     std::string m_name; 
     std::string m_password; 
}; 

Employee readEmployeeFromConsole() 
{ 
     std::string name, password; 
    std::cout << "Name: "; 
    std::cin >> name; 
    std::cout << "Password: " 
    std::cin >> password; 
    return Employee(name, password); 
} 

bool Employee::authenticate(const char * filename) const 
{ 
     // your implementation 
} 

int main() 
{ 
    Employee emp = readEmployeeFromConsole(); 
    if(emp.authenticate("input.txt")) 
    { 
     std::cout << "You're in!\n"; 
    } 
    else 
    { 
     std::cout << "Get out!\n"; 
    } 
} 
+0

thx代碼:) –