0

今天我开始学一点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;
    }

}

我想在他/她输入密码时验证用户,然后到 IF 部分,当我想编译它时,编译器返回状态“从'int'到'const char *'的无效转换[-fpermissive]|” ,请有人修复我的代码并解释我错了什么?

4

6 回答 6

4

请有人修复我的代码

在我们解释问题后尝试自己修复它。与我们发布您的代码相比,您将从中获得更多。

解释我错了什么

当然。该方法accesscheckpass需要一个std::stringas 参数(顺便说一句,您需要#include <string>在文件顶部)。你称之为

MyAccess.accesscheckpass(inputpass)

但是inputpass被声明为int inputpass;,所以它是 and int,而不是 a std::string。因此,您要么必须声明inputpass为 a string,要么了解如何将 a 转换int为 a 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);
    }

也应该返回 a bool

于 2012-10-23T10:46:12.663 回答
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() 仅处理真实条件,因此添加了错误条件。
于 2012-10-23T11:53:18.930 回答
0

accesscheckpass(string x)应该返回布尔值。

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

另外,inputpass应该是类型std::string

于 2012-10-23T10:46:27.697 回答
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;
            }
        }
};
于 2012-10-23T10:47:54.957 回答
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;
}

}

于 2012-10-23T11:14:23.393 回答
-2

您阅读字符形式std::cin。您需要将这些字符解析为整数。

于 2012-10-23T10:45:47.173 回答