我正在使用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);发布于 2020-10-17 03:26:43
数据类型的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
https://stackoverflow.com/questions/64394514
复制相似问题