首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ Nlohman::json第三方支持未编译

C++ Nlohman::json第三方支持未编译
EN

Stack Overflow用户
提问于 2020-10-17 02:25:24
回答 1查看 205关注 0票数 0

我正在使用nlohmann::json来(反)序列化JSON中的一些C++对象。我还不知道如何设置代码以与第三方库(SoapySDR)一起工作。在本例中,我的代码位于全局命名空间中,而SoapySDR代码位于其自己的命名空间中。在这个简化的例子中,我得到了大量的编译错误:

代码语言:javascript
复制
#include "json.hpp"

using nlohmann::json;

namespace SoapySDR
{

  class Range
  {
  public:
    double minimum(void) const;
    double maximum(void) const;
    double step(void) const;

  private:
    double _min, _max, _step;
  };

  class ArgInfo
  {
  public:
    Range range;
  };

}; // namespace SoapySDR

void to_json(json &j, const SoapySDR::Range &r)
{
  j += {"min",r.minimum()};
  j += {"max",r.maximum()};
  j += {"step",r.step()};
}

void from_json(const json &j, SoapySDR::Range &r)
{
//  r = SoapySDR::Range(j.at("min"), j.at("max"), j.at("step"));
}

void to_json(json &j, const SoapySDR::ArgInfo &ai)
{
  j = json{{"range", ai.range}};
}

void from_json(const json &j, SoapySDR::ArgInfo &ai)
{
  j.at("range").get_to(ai.range);
}

以下是错误消息,不包括有关尝试的扣除额的所有额外信息:

代码语言:javascript
复制
bob.cpp:41:31: error: no matching function for call to ‘nlohmann::basic_json<>::basic_json(<brace-enclosed initializer list>)’
bob.cpp:41:31: note:   couldn't deduce template parameter ‘JsonRef’
bob.cpp:41:31: note:   couldn't deduce template parameter ‘BasicJsonType’
bob.cpp:41:31: note:   couldn't deduce template parameter ‘CompatibleType’
   j = json{{"range", ai.range}};
bob.cpp:46:32: error: no matching function for call to ‘nlohmann::basic_json<>::get_to(SoapySDR::Range&) const’
   j.at("range").get_to(ai.range);
json.hpp:19588:28: error: no type named ‘type’ in ‘struct std::enable_if<false, int>’
                    int > = 0 >
json.hpp:19613:11: note:   template argument deduction/substitution failed:
bob.cpp:46:32: note:   mismatched types ‘T [N]’ and ‘SoapySDR::Range’
   j.at("range").get_to(ai.range);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-17 03:26:43

数据类型的to_jsonfrom_json重载必须位于类型的命名空间中,库才能找到它们:

代码语言:javascript
复制
namespace SoapySDR {

void to_json(json &j, const SoapySDR::Range &r)
{
  j += {"min",r.minimum()};
  j += {"max",r.maximum()};
  j += {"step",r.step()};
}

/* ... */

}

之后,您的代码将进行编译:https://godbolt.org/z/5cafYx

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64394514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档