JsonCpp只在Windows中出现模糊过载

Saf*_*fej 5 c++ windows json 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();
Run Code Online (Sandbox Code Playgroud)

它在Linux中工作正常,但是当我在Windows中尝试时,我有这个错误:

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

为什么会发生这种情况?如何解决这个问题?谢谢.

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

Maz*_*yod 4

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

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