2012-10-23 17 views
0

今天,我開始學習C++了一下,我開始用簡單的代碼來理解,但這個牌子讓我困惑C++ - 布爾返回值,那麼驗證它

#include <iostream> 

using namespace std; 

class MyVar 
{ 

    public: 
     MyVar(){ 
      cout << "this is constructor" << endl; 
     } 
     void accesscheckpass(string x){ 
      checkpass(x); 
     } 

    private: 
     bool checkpass(string x){ 
      if(x == "password"){ 
       return true; 
      } 
     } 
}; 

int main() 
{ 
    int inputpass; 
    cout << "please enter your password\n" << endl; 
    cin >> inputpass; 

    MyVar MyAccess; 

    if(MyAccess.accesscheckpass(inputpass) == true){ 
     cout << "welcome user 1" << endl; 
    }else{ 
     cout << "get lost !" << endl; 
    } 

} 

我想驗證用戶時,他/她輸入密碼,然後當我要編譯它時,編譯器返回狀態「無效轉換從'int'到'const char *'[-fpermissive] |」,請有人修復我的代碼並解釋什麼我錯了 ?

+0

檢查編譯器會報錯哪一行,然後看看你在那裏調用哪個函數,然後比較函數期望的類型和傳遞它的類型。 – PlasmaHH

回答

4

please someone repair my code

在我們解釋了什麼是錯的後,試着自己修復它。你會從中獲得比我們發佈你的代碼更多的東西。

explain what i'm wrong

當然。方法accesscheckpass預計std::string作爲參數(順便說一句,你需要在文件的頂部#include <string>)。你把它作爲

MyAccess.accesscheckpass(inputpass) 

inputpass被聲明爲int inputpass;,所以它和int,而不是一個std::string。因此,您必須聲明inputpassstring或瞭解如何將int轉換爲string。 (應該很容易)

而且,你的方法:

bool checkpass(string x){ 
     if(x == "password"){ 
      return true; 
     } 
    } 

只有當條件爲真返回。您應該有一個else分支以及:

else 
     return false; 

或者更好的是,直接返回結果。

bool checkpass(string x){ 
     return x == "password"; 
    } 

和你的方法

bool accesscheckpass(string x){ 
     return checkpass(x); 
    } 

應該返回bool爲好。

+0

我犯這麼多錯誤,很遺憾,謝謝你的詳細解釋...... :) –

-2

您閱讀的字符格式爲std::cin。您需要將這些字符解析爲一個整數。

+0

哈哈,我犯了一個多麼可惜的錯誤,現在我編輯它並將輸入通道轉換爲字符串,但是這個錯誤再次出現.. 錯誤:無效的操作數類型爲'void'和'bool'爲二進制操作符== 「| –

+0

@MohdShahril然後投我的答案:-) – logoff

+0

@MohdShahril閱讀其他很好解釋的答案。 Luchian祝賀你的投票和你的評論... – logoff

0

accesscheckpass(string x)應該返回bool。

bool accesscheckpass(string x){ 
     return checkpass(x); 
    } 

此外,inputpass應類型的std::string

0

變化:

int inputpass 

string inputpass 

您的密碼是一個字符串,而不是一個整數。

此外,你的班級充滿了錯誤!它可能應該是這個樣子:

class MyVar 
{ 

    public: 
     MyVar(){ 
      cout << "this is constructor" << endl; 
     } 
     bool accesscheckpass(string x){ 
      return checkpass(x); 
     } 

    private: 
     bool checkpass(string x){ 
      if(x == "password"){ 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
}; 
0

下面是代碼,你需要爲這個,這不是我們的最佳代碼,但它的工作原理:

#include <iostream> 
#include <string> 
#include <sstream> 

using namespace std; 

class MyVar 
{ 

public: 
    MyVar(){ 
     cout << "this is constructor" << endl; 
    } 
    bool accesscheckpass(string x){ 
     return checkpass(x); 
    } 

private: 
    bool checkpass(string x){ 
     string pass = "password"; 
     for(int i = 0; i < x.length();i++) 
      if(x[i]!=pass[i]) 
       return false; 
     return true; 
    } 
    }; 

    int main() 
    { 
string inputpass; 
cout << "please enter your password\n" << endl; 
getline(cin, inputpass); 
MyVar MyAccess; 

if(MyAccess.accesscheckpass(inputpass) == true){ 
    cout << "welcome user 1" << endl; 
}else{ 
    cout << "get lost !" << endl; 
} 

}

1
#include <iostream> 
#include <string> 

using namespace std; 

class MyVar 
{ 

    public: 
     MyVar(){ 
      cout << "this is constructor" << endl; 
     } 

      bool accesscheckpass(string x){ 
      return checkpass(x); 
     } 

    private: 
     bool checkpass(string x){ 

      if(x == "password"){ 
       return true; 
      } 
      else 
      return false; 
     } 
}; 

int main() 
{ 
    string inputpass; 
    cout << "please enter your password\n" << endl; 
    cin >> inputpass; 

    MyVar MyAccess; 

    if(MyAccess.accesscheckpass(inputpass) == true){ 
     cout << "welcome user 1" << endl; 
    }else{ 
     cout << "get lost !" << endl; 
    } 

} 

以上是更正的工作代碼。發現錯誤如下:

  1. 「無效的從'int'轉換爲'const char *'[-fpermissive] |」因爲您將int傳遞給其參數是字符串類型參數的函數,因此inputpass的數據類型已更改。
  2. accesscheckpass()不返回任何內容,所以我返回checkpass()的返回值。 checkpass()只處理真實情況,所以添加了錯誤的聯繫。