C++编译时引入json/nlohmann文件报错

17 篇文章 0 订阅
1 篇文章 0 订阅

报错信息:

In file included from /home/chenlang/catkin_ws/src/leanrning_communication/src/mysql/../utils/data.h:14:0,
                 from /home/chenlang/catkin_ws/src/leanrning_communication/src/mysql/MyRobotDb.h:32,
                 from /home/chenlang/catkin_ws/src/leanrning_communication/src/mysql/MyRobotDb.cpp:1:
/usr/include/json.hpp: In function ‘bool nlohmann::operator<(nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::const_reference, nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::const_reference)’:
/usr/include/json.hpp:6057:62: error: wrong number of template arguments (1, should be 2)
                     return *lhs.m_value.array < *rhs.m_value.array;
                                                              ^~~~~
In file included from /usr/include/c++/7/tuple:39:0,
                 from /usr/include/c++/7/mutex:38,
                 from /home/chenlang/catkin_ws/src/leanrning_communication/src/mysql/MyRobotDb.h:10,
                 from /home/chenlang/catkin_ws/src/leanrning_communication/src/mysql/MyRobotDb.cpp:1:
/usr/include/c++/7/array:94:12: note: provided for ‘template<class _Tp, long unsigned int _Nm> struct std::array’
     struct array
            ^~~~~
leanrning_communication/CMakeFiles/httpserver.dir/build.make:140: recipe for target 'leanrning_communication/CMakeFiles/httpserver.dir/src/mysql/MyRobotDb.cpp.o' failed
make[2]: *** [leanrning_communication/CMakeFiles/httpserver.dir/src/mysql/MyRobotDb.cpp.o] Error 1
make[2]: *** 正在等待未完成的任务....
CMakeFiles/Makefile2:1281: recipe for target 'leanrning_communication/CMakeFiles/httpserver.dir/all' failed
make[1]: *** [leanrning_communication/CMakeFiles/httpserver.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j20 -l20" failed

错误代码信息:

修改后代码信息:

问题分析 :

  1. 预处理阶段(Preprocessing)

    • 在预处理阶段,编译器会处理 #include 指令,将相应的头文件内容插入到源文件中。在第一段代码中,<iostream><json.hpp> 分别作为独立的指令处理,导致预处理后的源文件中包含了 <iostream><json.hpp> 的内容。
    • 在第二段代码中,<iostream>using namespace std; 被作为一组处理,而 <json.hpp>using namespace nlohmann; 又被作为另一组处理。这种组合不是标准的语法,导致编译器可能无法正确处理这种指令的顺序和作用域,因此可能会出现错误。
  2. 命名空间引入的影响

    • C++中,using namespace 语句应该在全局或函数作用域内使用,而不应该在 #include 指令之间插入。这是因为 using namespace 实际上是一个声明,编译器需要根据它来确定后续代码中名称的解析方式。在第二段代码中,using namespace std;using namespace nlohmann;#include 指令之间被插入,这不符合语法规范,可能导致编译器无法正确解析后续代码。
  3. 编译器错误

    • 当编译器无法正确处理 using namespace 的位置或者指令顺序时,会导致编译错误。例如,可能会出现无法识别的命名空间或者重复定义的错误,这取决于具体的编译器和头文件内容。

总结

  • 错误原因在于#include预处理指令还没有处理完,就插入了命名空间的内容,打乱了指令顺序和作用域,可能json的某些模板指向了std的命名空间.

`nlohmann::json` 是一个流行的 C++ JSON 库,它提供了一个非常方便的方式来处理 JSON 数据。这个库被广泛使用在 C++ 项目中,用于解析、序列化和反序列化 JSON 数据。通过包含 nlohmannJSON 库,你可以轻松地将 C++ 数据结构转换为 JSON 格式,或者将 JSON 字符串转换为 C++ 数据结构。 在 C++ 中使用 `nlohmann::json` 库通常涉及以下几个步骤: 1. 包含库头文件:首先,你需要在你的 C++ 文件中包含 `nlohmann::json` 库的头文件,通常只需要一行代码:`#include <nlohmann/json.hpp>`。 2. 创建 JSON 对象:你可以创建 `json` 类型的对象,并使用它来构建 JSON 结构。例如,你可以创建一个空的 JSON 对象或者数组,或者使用花括号初始化器语法来创建包含数据的 JSON 对象。 3. 使用 JSON 对象:你可以使用操作符、成员函数等来访问和修改 JSON 对象的内容。你可以添加、删除键值对,或者查询它们。 下面是一个简单的使用示例: ```cpp #include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; // 定义一个别名,简化后续使用 int main() { // 创建一个 JSON 对象 json j = { {"name", "John Doe"}, {"age", 35}, {"is_student", false} }; // 输出这个 JSON 对象 std::cout << j.dump(4) << std::endl; // 修改 JSON 对象中的某个值 j["age"] = 36; // 输出修改后的 JSON 对象 std::cout << j.dump(4) << std::endl; return 0; } ``` 在这个例子中,我们首先创建了一个包含几个键值对的 JSON 对象。然后我们输出了这个对象的格式化字符串表示,并修改了其中一个值。最后,我们再次输出了对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值