首页 > 解决方案 > 比较if语句中的字符串时如何修复'operator =='不匹配

问题描述

尝试使用 if 语句比较字符串,但出现运算符错误。如何解决此问题以使语句比较字符串?使用 Code::Blocks 作为编译器。

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

(注意:原始代码中的大部分注释都是为了实验,总体上存在大量错误,我可以稍后再讨论)

这是我试图为井字游戏创建的排行榜。我尝试了不同的方法来比较字符串以解决此问题,例如 .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}')

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

标签: c++operatorsfilestream

解决方案


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

转动:

void admin (string names, ...

进入:

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

推荐阅读