关于字符串函数strcmp的应用
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name,note;
cin>>name>>note;
if(!strcmp(name,note))
{cout<<"ture";}
return 0;
}
这个程序很简单 但是在if(!strcmp(name,note)) 这句里出错了
错误提示是:'strcmp' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
我也不知道是哪里出问题了
大家帮帮忙吧 谢谢啦
[解决办法]
strcmp函数要求两个参数为两个指向字符类型的指针,是char*
你给的两个参数是两个字符串模板类,是std::string
编译器无法从后者隐式转换到前者,因此报错
[解决办法]
if(!strcmp(name.c_str(),note.c_str()))
string类型和const char *类型无法隐式转换,需要用c_str()