c++ - 仅在 Windows 中的 JsonCpp 模糊重载

标签 c++ json windows overloading ambiguous

我需要使用 jsoncpp 库读取一个 json 文件。

我有这个文件:

{"one":false,"two":[{"id":"first"},{"id":"second"}],"three":550}

如果我只需要读取“two”元素的第一个 id,我使用:

std::string contents; //Contain the file
Json::Value root;
Json::Reader reader;
reader.parse(contents, root, false);
std::string aux = root["two"][0]["id"].asString();

它在 Linux 中运行良好,但当我在 Windows 中尝试时出现此错误:

错误:'root.Json::Value::operator[](((const char*)"two"))[0]' 中 'operator[]' 的不明确重载

为什么会发生这种情况,我该如何解决?谢谢。

已解决:有两个operator[],一个以int为参数,另一个以const char为参数,并且编译器不知道在 Windows 中使用谁,但在 Linux 中是。现在我使用 [0u] 而不是 [0] 来指示一个数字作为参数并且它工作正常。

最佳答案

这把我逼疯了,直到我偶然发现这个问题,我才神秘地想通了: 只需将其转换为 Json::Uint(出于某种原因),或使用 'u':

MapEvent::MapEvent(Json::Value& val)
{
    operator_enum = val.get(0u, "").asString();
}

关于c++ - 仅在 Windows 中的 JsonCpp 模糊重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18901781/

相关文章:

c++ - 在QT中读取窗口的注册表

c++ - 从 std::ranges 算法中获取投影值

c++ - OF_SHARE_DENY_NONE 不分享阅读?

ajax - 如何在 Django View 中解析嵌套的 json ajax 对象?

windows - 在批处理脚本中生成带有时间戳的唯一文件名

asp.net - 您如何处理在 IIS 上运行的网站的计划任务?

c++ - 具有分配语义的非拥有者

c++ - OpenGL 多线程/共享上下文和 glGenBuffers

java - 无法用Java解析JSON文件

javascript - 将 json 发送到网格