是什么原因?
The error message “no viable overloaded ‘=’ “ in the context of NLohamman’s JSON library (or any C++ code) typically means that you are trying to assign a value to an object or variable, but the compiler can’t find a suitable assignment operator for that particular operation.
In the case of jstar[key]=value;, it seems like you’re trying to assign a value to a key in a JSON object using syntax similar to JavaScript. However, JSON manipulation in C++ libraries usually doesn’t use this kind of syntax directly.
NLohmann’s JSON library (nlohmann/json) provides functions such as operator[] for accessing and assigning values, but not with the exact syntax you’ve used. Here’s how you’d set a value with a string key in his library:
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// ...
json jstar;
jstar["key"] = value; // where 'value' is the data you want to insert
Make sure value is a type that can be converted into a type supported by the JSON library, such as int, double, std::string, bool, or another json object.
nlohamman json jstar[key]=value; no viable overloaded ‘=‘
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- GISer Liu 2024-02-22 21:29关注
该回答引用自GPT-3.5,由博主GISer Liu编写:
针对你提出的问题,错误信息"no viable overloaded '=' "通常意味着你试图给一个对象或变量赋值,但编译器找不到适当的赋值运算符来执行这个操作。在你的情况下,似乎你正在尝试使用类似JavaScript的语法给JSON对象中的键赋值。然而,在C++库中操作JSON通常不直接使用这种语法。
nlohmann/json库提供了operator[]等函数来访问和赋值数值,但不是用你使用的确切语法。下面是如何在该库中使用字符串键设置数值的方法:
#include <nlohmann/json.hpp> using json = nlohmann::json; // ... json jstar; jstar["key"] = value; // 这里的'value'是你想要插入的数据
确保value是一种可以转换为JSON库支持的类型,比如int、double、std::string、bool或另一个json对象。
这样,你可以按照这种方式修改你的代码,以解决该错误。
如果该回答解决了您的问题,请采纳!如果没有,请参考以下方案进行修订
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 对于squad数据集的基于bert模型的微调
- ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
- ¥20 steam下载游戏占用内存
- ¥15 CST保存项目时失败
- ¥15 树莓派5怎么用camera module 3啊
- ¥20 java在应用程序里获取不到扬声器设备
- ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
- ¥15 Attention is all you need 的代码运行
- ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
- ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: