You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HTTPRequest库编译JSON解析代码时遇operator!=匹配错误求助

解决HTTPRequest库中response.status与int比较的编译错误

问题分析

编译错误提示const http::Status类型无法直接和int类型(200)比较,说明response.status是http::Status枚举类型,枚举类型与整数不能直接进行比较操作。

正确解决方案

根据该库的定义,可通过以下两种方式修正代码:

方式1:使用库定义的枚举常量

确保引用正确的命名空间与枚举值,http::Status枚举中代表成功的状态常量是http::Status::OK,而非http::OK:

if (response.status != http::Status::OK) {
    std::cerr << "Error" << std::endl;
    continue;
}

方式2:将枚举转换为底层整数类型

如果需要直接和200比较,可将枚举值强制转换为int类型:

if (static_cast<int>(response.status) != 200) {
    std::cerr << "Error" << std::endl;
    continue;
}

之前尝试无效的原因

  • 改成response.status()无效:该库的响应对象中status是成员变量而非成员函数,不存在status()方法。
  • 用http::OK无效:库中OK状态的枚举值属于http::Status枚举类,正确引用方式为http::Status::OK。

内容的提问来源于stack exchange,提问作者pldz2lhju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 17:44:55