简体   繁体   English

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

[英]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.

相关问题 不存在从标准字符串到const char *的合适转换函数 - No suitable conversion function from std string to const char * exists 不存在从“std::string”到“const char *”的合适转换函数 - No suitable conversion function from “std::string” to “const char *” exists 如何修复“没有合适的转换函数从”String“到”const char *“存在”? - How to fix “no suitable conversion function from ”String“ to ”const char *“ exists”? 不存在从“ std :: string”到“ char”的适当转换 - no suitable conversion from “std::string” to “char” exists 没有合适的转换 function 从 (C++) std::istream &amp;std::getline <char, std::char_traits<char> , 标准::分配器<char> &gt; 布尔值</char></char,> - no suitable conversion function from (C++) std::istream &std::getline<char, std::char_traits<char>, std::allocator<char>> to bool C ++和Visual Studio错误-没有来自“ std :: basic_ostream的合适转换函数” <char, std::char_traits<char> &gt;”到“ int”存在 - C++ and Visual Studio error - no suitable conversion function from “std::basic_ostream<char, std::char_traits<char>>” to “int” exists 从“ const std :: string”到“ time_t”的转换函数不存在 - No suitable conversion function from “const std::string” to “time_t” exists C++ 没有合适的转换 function from to 存在 - C++ no suitable conversion function from to exists IntelliSense:不存在从“ std :: string”到“ char”的合适转换函数,那么我该如何处理? - IntelliSense: no suitable conversion function from “std::string” to “char” exists so how do i go about this? 从“ std :: string”到“ PVOID”的转换功能不存在 - no suitable conversion function from “std::string” to “PVOID” exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM