c++ 不存在从 "std::string" 到 "const char*" 的合适的转换函数

c++ no suitable conversion function from "std::string" to "const char*" exists

如题所示,是我在执行我的程序时,在密码函数的某处出错。实际上它是一个基本的密码函数,在 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");
  }

if语句中的表达式

if (strcmp(pass, "sha") == 0) 

不正确。

该函数需要类型为 const char * 的两个参数,而您提供了类型为 std::string 的第一个参数,并且没有从类型 std::string 到类型 const char 的隐式转换*.

改用

if ( pass == "sha" ) 

在这种情况下,由于非-显式构造函数

basic_string(const charT* s, const Allocator& a = Allocator());

您还可以将 string 转换为 const char* 并与 strcmp 进行比较。

if (strcmp(pass.c_str(), "sha") == 0)