C++ Nlohman::json 第三方支持不编译
C++ Nlohman::json third-party support not compiling
我正在使用 nlohmann::json 来(反)序列化 JSON 中的某些 C++ 对象。我还不明白如何设置代码以使用第三方库 (SoapySDR)。在此示例中,我的代码位于全局命名空间中,而 SoapySDR 代码位于其自己的命名空间中。我在这个简化示例中遇到了大量编译错误:
#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);
}
这些是错误消息,不包括有关尝试扣除的所有额外信息:
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);
数据类型的 to_json
和 from_json
重载需要位于类型的命名空间中,库才能找到它们:
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
我正在使用 nlohmann::json 来(反)序列化 JSON 中的某些 C++ 对象。我还不明白如何设置代码以使用第三方库 (SoapySDR)。在此示例中,我的代码位于全局命名空间中,而 SoapySDR 代码位于其自己的命名空间中。我在这个简化示例中遇到了大量编译错误:
#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);
}
这些是错误消息,不包括有关尝试扣除的所有额外信息:
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);
数据类型的 to_json
和 from_json
重载需要位于类型的命名空间中,库才能找到它们:
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