为什么 NULL 未声明?
当我尝试编译这段代码时,我遇到了这个结构构造函数的问题:
typedef struct Node
{
Node( int data ) //
{
this->data = data;
previous = NULL; // Compiler indicates here
next = NULL;
}
int data;
Node* previous;
Node* next;
} NODE;
当我来的时候,出现了这个错误:
\linkedlist\linkedlist.h||In constructor `Node::Node(int)':|
\linkedlist\linkedlist.h|9|error: `NULL' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|
最后一个问题是结构,但是当它在我的 main.cpp 中时它工作得很好,这次它在头文件中并且给了我这个问题。 我正在使用 Code::Blocks 来编译此代码
I have a problem with this struct contructor when I try to compile this code:
typedef struct Node
{
Node( int data ) //
{
this->data = data;
previous = NULL; // Compiler indicates here
next = NULL;
}
int data;
Node* previous;
Node* next;
} NODE;
when I come this error occurs:
\linkedlist\linkedlist.h||In constructor `Node::Node(int)':|
\linkedlist\linkedlist.h|9|error: `NULL' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|
Last problem was the struct, but it worked fine when it was in my main.cpp, this time it's in a header file and is giving me this problem. I am using Code::Blocks to compile this code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
NULL
不是 C 或 C++ 语言中的内置常量。 事实上,在 C++ 中它或多或少已经过时了,只需使用普通文字0
代替,编译器将根据上下文执行正确的操作。在较新的 C++(C++11 及更高版本)中,使用 nullptr(如评论中指出的,谢谢)。
否则,添加
#include <stddef.h>
获取 NULL 定义。
NULL
is not a built-in constant in the C or C++ languages. In fact, in C++ it's more or less obsolete, just use a plain literal0
instead, the compiler will do the right thing depending on the context.In newer C++ (C++11 and higher), use
nullptr
(as pointed out in a comment, thanks).Otherwise, add
#include <stddef.h>
to get the
NULL
definition.一定要使用 NULL。 无论如何,它只是 #define 为 0,并且在语义上将其与整数 0 区分开来非常有用。
使用 0(因此为 NULL)存在问题。 例如:
C++ (C++0x) 的下一个版本包含
nullptr
来修复此问题。Do use NULL. It is just #defined as 0 anyway and it is very useful to semantically distinguish it from the integer 0.
There are problems with using 0 (and hence NULL). For example:
The next version of C++ (C++0x) includes
nullptr
to fix this.您在此文件中包含“stdlib.h”或“cstdlib”吗? NULL 在 stdlib.h/cstdlib 中定义
或
Are you including "stdlib.h" or "cstdlib" in this file? NULL is defined in stdlib.h/cstdlib
or
NULL
不是核心 C++ 语言的本机部分,但它是标准库的一部分。 您需要包含包含其定义的标准头文件之一。#include
或#include
应该足够了。如果包含
cstddef
或stddef.h
,则保证NULL
的定义可用。 不能保证,但如果您包含许多其他标准标头,则很可能会包含其定义。NULL
isn't a native part of the core C++ language, but it is part of the standard library. You need to include one of the standard header files that include its definition.#include <cstddef>
or#include <stddef.h>
should be sufficient.The definition of
NULL
is guaranteed to be available if you includecstddef
orstddef.h
. It's not guaranteed, but you are very likely to get its definition included if you include many of the other standard headers instead.不要使用
NULL
,C++ 允许您使用未经修饰的0
来代替:并且,与 C++11 一样,您通常不应使用
NULL
或0
,因为它为您提供了std::nullptr_t
类型的nullptr
,即更适合该任务。Don't use
NULL
, C++ allows you to use the unadorned0
instead:And, as at C++11, you generally shouldn't be using either
NULL
or0
since it provides you withnullptr
of typestd::nullptr_t
, which is better suited to the task.