[英]c++ no suitable conversion function from "std::string" to "const char*" exists
the question as the title suggests, is an error when i was executing my program in the certain part of the password function.正如标题所暗示的那样,当我在密码功能的某些部分执行我的程序时出现错误。 actually it is a basic password function which was working properly in turbo c++, but in visual c++ this error arrives实际上它是一个基本的密码功能,在 turbo c++ 中正常工作,但在 Visual c++ 中出现此错误
void user::password()
{
char any_key, ch;
string pass;
system("CLS");
cout << "\n\n\n\n\n\n\n\n\t\t\t\t*****************\n\t\t\t\t*ENTER
PASSWORD:*\n\t\t\t\t*****************\n\t\t\t\t";
start:
getline(cin,pass);
if (strcmp(pass, "sha") == 0) //this is where the error is!*
{
cout << "\n\n\t\t\t\t ACCESS GRANTED!!";
cout << "\n\t\t\t PRESS ANY KEY TO REDIRECT TO HOME PAGE";
cin >> any_key;
}
else
{
cout << "\n\t\t\t\t ACCESS DENIED :(,RETRY AGAIN!!\n\t\t\t\t";
goto start;
}
system("CLS");
}
The expression in the if statement if 语句中的表达式
if (strcmp(pass, "sha") == 0)
is incorrect.是不正确的。
The function requires the both parameters of the type const char * while you supplied the first argument of the type std::string and there is no implicit conversion from the type std::string to the type const char *.当您提供 std::string 类型的第一个参数时,该函数需要 const char * 类型的两个参数,并且没有从 std::string 类型到 const char * 类型的隐式转换。
Use instead改用
if ( pass == "sha" )
In this case there is an implicit conversion from the type const char * (the type of the string literal after its implicit conversion from the array type) to an object of the type std::string due to the non-explicit constructor在这种情况下,由于非显式构造函数,存在从类型 const char *(从数组类型隐式转换后的字符串文字的类型)到 std::string 类型的对象的隐式转换
basic_string(const charT* s, const Allocator& a = Allocator());
您还可以将字符串转换为const char*并与 strcmp 进行比较。
if (strcmp(pass.c_str(), "sha") == 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.