[英]No matching function for call to strcmp
I am new to c++ and currently am facing an error while using strcmp
. 我是c ++的新手,目前在使用strcmp
时遇到错误。
I have defined a structure as follows: 我已经定义了如下结构:
struct student
{
string name;
int roll;
float marks;
dob dobi;
string dobp;
};
student *p;
And then, I am passing the pointer inside a function to sort it, like this: 然后,我将指针传递给函数以对其进行排序,如下所示:
void sortData(student *p)
{
int a=0,b=0;
for (a=0; a<=arraySize; a++)
{
for (b=a; b<=arraySize; b++)
{
if( strcmp(p[a].name, p[b].name) > 0 ) //Error
{
//sort logic yet to be implemented
}
}
}
}
Can someone please point out the mistake. 有人可以指出错误。
Error Message: 错误信息:
No matching function for call to
strcmp
没有用于调用strcmp
匹配函数
strcmp
takes two const char*
s for input - you need to convert your strings to C-style strings (assuming you're using std::string
) using std::string::c_str()
: strcmp
需要两个const char*
s作为输入 - 你需要使用std::string::c_str()
将你的字符串转换为C风格的字符串(假设你正在使用std::string
std::string::c_str()
:
if (strcmp(p[a].name.c_str(), p[b].name.c_str()) > 0)
// ^ Here ^ and here
std::strcmp
takes const char*
as its parameter, while std::string
doesn't match directly. std::strcmp
将const char*
作为参数,而std::string
不直接匹配。
Because you're using std::string
, you can just use operator>(std::basic_string)
因为你正在使用std::string
,你可以使用operator>(std::basic_string)
if (p[a].name > p[b].name)
or use std::basic_string::compare
或者使用std::basic_string::compare
if (p[a].name.compare(p[b].name) > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.