error: ‘NULL’ was not declared in this scope
In file included from /home/topeet/qts/SanliApp/views/laserprams/myuis/myuilaserpublic.h:3,
from /home/topeet/qts/SanliApp/views/laserprams/myuis/myuilaserpublic.cpp:1:
/home/topeet/qts/SanliApp/views/mains/../../publics/mocro.h:6:42: error: ‘NULL’ was not declared in this scope
6 | #define initMy(AType) AType* AType::my = NULL; AType* AType::getMy(){ if(my==NULL){ new AType();} ;return my; };
| ^~~~
/home/topeet/qts/SanliApp/views/laserprams/myuis/myuilaserpublic.cpp:3:1: note: in expansion of macro ‘initMy’
3 | initMy(MyUiLaserPublic)
| ^~~~~~
/home/topeet/qts/SanliApp/views/laserprams/myuis/myuilaserpublic.cpp:2:1: note: ‘NULL’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
1 | #include "myuilaserpublic.h"
+++ |+#include <cstddef>
2 |
错误信息指出在文件 mocro.h
中使用了 NULL
,但是 NULL
在当前的作用域中没有被声明。NULL
通常是在 C++ 标准库的头文件 <cstddef>
或 <cstdlib>
中定义的。
根据错误提示,你可以通过在 mocro.h
或在使用 mocro.h
之前包含 <cstddef>
来解决这个问题。这样可以确保 NULL
被正确地定义。
具体修改如下:
- 打开
mocro.h
文件。 - 在文件的开始部分添加
#include <cstddef>
。这行代码应该放在其他包含指令之前,以确保NULL
在使用前已经被定义。
修改后的 mocro.h
文件应该类似于这样:
#include <cstddef> // 包含对 NULL 的定义
#define initMy(AType) AType* AType::my = NULL; AType* AType::getMy(){ if(my==NULL){ new AType();} ;return my; };
或者,如果你不想修改 mocro.h
,你也可以在使用这个宏的每个文件中确保包含了 <cstddef>
。例如,在 myuilaserpublic.cpp
中:
#include <cstddef>
#include "myuilaserpublic.h"
这样也能解决问题。选择哪种方式取决于你对项目结构的偏好以及 mocro.h
被使用的广泛程度。如果 mocro.h
被多个文件依赖,建议在 mocro.h
中添加 #include <cstddef>
。