我最近将Windows C++应用程序转换成Linux C++应用程序,并使用Debian的Windows子系统将其编译为Linux。但是,通过使用json库nlohmann
no match for 'operator-' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<double> >::value_type {aka double}' and 'nlohmann::basic_json<>::value_type {aka nlohmann::basic_json<>}')
例如,当我在json元素和double之间使用运算符时,到处都会出现此错误。例如:
MSE_total += pow(ref.z[j*multiplier] - actual[j]["z"], 2) / pow(ref.z[j*multiplier], 2);
这一行给出了上面的错误。我应该明确说明json中的变量类型吗?我该怎么做?
最佳答案
有no operator-
taking a nlohmann::basic_json
。我想是吧
ref.z[j*multiplier] - actual[j]["z"]
期望通过其
actual[j]["z"]
将double
转换为operator ValueType()
。。。它应该(在底层类型不匹配的情况下抛出type_error.302
)。为什么不呢?我打赌nlohmann的json版本号与您的windows和linux版本不同。
解决方法:cast that value to a double(
actual[j]["z"].get<double>()
)。