如何修复"运算符不匹配=="比较if语句中的字符串时

How to fix 'no match for 'operator=='' when comparing strings in if statement

本文关键字:字符串 语句 比较 何修复 运算符 不匹配 if      更新时间:2023-10-16

尝试使用if语句比较字符串,但遇到运算符错误。如何修复此问题以使语句比较字符串?使用代码::块作为编译器。

完整的原始代码粘贴:https://pastebin.com/DJGqJBwu(由于问题已解决,不再公开)

(注意:原始代码中的大多数注释都是为了实验,总的来说有很多错误,我稍后可以查看)

这是一个排行榜,我正试图创建一个Tic-Tac-Toe游戏。我尝试了不同的方法来比较字符串来解决这个问题,比如.compare.

void admin (string names, int nameW[10], int wins[10], unsigned int a[10]) //called variables from main
{
char repeat = 'y'; //used for function loop
char resetAll; //used elsewhere
int winCount; //used elsewhere
string reset;
string modify; //used elsewhere
string admin; //used elsewhere
cin >> reset;
//Reset
if (reset==names[0])
{
wins[0] = 0;
}
}

构建日志:

error: no match for 'operator==' (operand types are 'std::__cxxll::string {aka std::__cxll::basic_string<char>}' and '__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}')

我希望重置变量与names[0]变量相同,以便代码可以执行。结果是代码无法构建和运行(无法测试程序)。

问题是管理函数参数中的字符串"names"没有声明数组。

转弯:

void admin (string names, ...

进入:

void admin (string name[10], ...