我是 c++ 的新手,目前在使用 strcmp
时遇到错误。
我定义了一个结构如下:
struct student
{
string name;
int roll;
float marks;
dob dobi;
string dobp;
};
student *p;
然后,我将指针传递到函数内部以对其进行排序,如下所示:
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
}
}
}
}
谁能指出错误
错误信息:
No matching function for call to
strcmp
最佳答案
strcmp
需要两个 const char*
作为输入——您需要将您的字符串转换为 C 风格的字符串(假设您使用的是 std::string
) 使用 std::string::c_str()
:
if (strcmp(p[a].name.c_str(), p[b].name.c_str()) > 0)
// ^ Here ^ and here
关于c++ - 没有用于调用 strcmp 的匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376303/