2014-02-14 54 views
0

部首通過矢量搜索時出錯?

#ifndef BBOARD_H 
#define BBOARD_H 

#include <iostream> 
#include <string> 
#include <vector> 
using namespace std; 
class User{ 
}; 
class Message{ 
}; 
class BBoard{ 
private: 
    string title; 
    vector<User> user_list; 
    User current_user; 
    vector<Message> message_list; 
public: 
    BBoard(); 
    BBoard(const string &ttl); 
    void setup(const string &input_file); 
    void login(); 
    void run(); 
private: 
    bool user_exists(const string &name, const string &pass) const; 
}; 

#endif 

cpp文件

#include "BBoard.h" 
#include <fstream> 
#include <algorithm> 
using namespace std; 

User user_l; 
BBoard::BBoard(){ 
    title = "Hello World"; 
    vector<User> user_list; 
    User current_user; 
    vector<Message> message_list; 
} 

BBoard::BBoard(const string &ttl){ 
    title = ttl; 
} 

void BBoard::setup(const string &input_file){ 
    ifstream fin; 
    fin.open("users1.txt"); 
    while(!fin.eof()){ 
     user_list.push_back(user_l); 
    } 
} 

bool BBoard::user_exists(const string &name, const string &pass) const{ 
    vector<User>::iterator i = find(user_list.begin(), user_list.end(), name); 
} 

void BBoard::login(){ 
    string sn, pw; 
    cout << "Welcome to " << title << endl; 
    bookmark: 
    cout << "Enter our username ('Q' or 'q' to quit): "; 
    getline(cin, sn); 
    cout << "Enter your password: ('Q' or 'q' to quit): "; 
    getline(cin, pw); 

} 

我user_exists功能不斷給出關於沒有合適的用戶定義的轉換錯誤。我試圖使用user_exists來檢查用戶名和密碼,如果它存在,它將返回true。

+3

也許你應該告訴我們**確切**錯誤。 – 0x499602D2

回答

2

您的user_exists功能是bool,但不返回任何值。這裏有個建議:

bool BBoard::user_exists(const string &name, const string &pass) const{ 
    vector<User>::const_iterator i = find(user_list.begin(), user_list.end(), name); 
    return !(i == user_list.end()); 
} 
+0

此外,他/她需要使用const_iterator,因爲該方法被聲明爲const。 –

+0

更好的是,使它成爲函數類/結構體... –

+1

'if(x)return false;返回true;'不愉快。改爲使用'return!x;'。 – Roddy