c++ - 比较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] 变量相同,以便代码可以执行。结果是代码无法构建和运行(无法测试程序)。
解决方案
问题是字符串“names”没有声明数组的管理函数参数。
转动:
void admin (string names, ...
进入:
void admin (string name[10], ...
推荐阅读
- blazor - 使用 Blazor 应用程序随机出现“错误:电路无法初始化”
- python - 检查文件是否存在时防止“GUI 冻结”
- swagger - react-admin 中的 OpenAPI 支持?
- ios - Ios react-native 无法部署到应用商店获取 ITMS-90683:Info.plist 中缺少目的字符串
- java - 我可以用 Twilio 中的 call.getSubresourceUris() 和 call.getUri() 做什么
- blockchain - 如果一个智能合约从另一个智能合约中读取数据,它会花费 gas 吗?
- reactjs - JSX 元素类型“App”没有任何构造或调用签名
- java - 在 Main 类中删除 recyclerview 卡时,如何从 recyclerview 适配器类类更新 sharedpreferences?
- state - 如何在 SwiftUI 中从自定义初始化设置状态值
- python-3.x - 熊猫数据框分箱和分组