【问题标题】:JsonCpp ambiguous overload only in Windows仅在 Windows 中的 JsonCpp 模棱两可的重载
【发布时间】:2013-09-19 17:57:18
【问题描述】:

我需要用 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 中尝试时出现此错误:

error: ambiguous overload for 'operator[]' in 'root.Json::Value::operator[](((const char*)"two"))[0]'

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

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

【问题讨论】:

    标签: c++ json windows overloading ambiguous


    【解决方案1】:

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

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

    【讨论】:

    • 这很烦人,可能是 API 错误,但至少提到了in the docs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2012-07-14
    • 2019-04-27
    相关资源
    最近更新 更多