【问题标题】:Nlohmann's json library, json array to a vector of structs with pointers inside the structNlohmann json 库,json 数组到结构体的向量,结构体内部有指针
【发布时间】:2020-05-09 21:32:28
【问题描述】:

我看到this 发布了关于将 json 数组转换为结构向量的帖子。我有一个名为 Foo 的结构:

typedef struct Foo {
  Foo* SubFoo;
} Foo;

当我尝试这样做时:

void from_json(const nlohmann::json& j, Foo& f) {
    j.at("SubFoo").get_to(f.SubFoo);
}

它给了我这个错误:

error: no matching function for call to 'nlohmann::basic_json<>::get_to(Foo*&) const'
 j.at("SubFoo").get_to(a.SubFoo);

那么我怎样才能从 json 中获取一个指向值的指针呢?

【问题讨论】:

    标签: c++ json pointers struct nlohmann-json


    【解决方案1】:

    只需取消引用指针:

    void from_json(const nlohmann::json& j, Foo& f) {
        j.at("SubFoo").get_to(*f.SubFoo);
    }
    

    这是demo

    你必须确保你没有取消引用无效的指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      相关资源
      最近更新 更多