C++ 为什么未声明 NULL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/924664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is NULL undeclared?
提问by unwind
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
最后一个问题是结构,但是当它在我的 main.cpp 中时它工作正常,这次它在一个头文件中并且给了我这个问题。我正在使用 Code::Blocks 来编译这段代码
回答by unwind
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 literal 0
instead, the compiler will do the right thing depending on the context.
NULL
不是 C 或 C++ 语言中的内置常量。事实上,在 C++ 中它或多或少已经过时了,只需使用普通文字0
代替,编译器会根据上下文做正确的事情。
In newer C++ (C++11 and higher), use nullptr
(as pointed out in a comment, thanks).
在较新的 C++(C++11 及更高版本)中,使用nullptr
(如评论中指出的,谢谢)。
Otherwise, add
否则,添加
#include <stddef.h>
#include <stddef.h>
to get the NULL
definition.
得到NULL
定义。
回答by unwind
Do use NULL. It is just #defined as 0 anyway and it is very useful to semantically distinguish it from the integer 0.
请使用 NULL。无论如何,它只是 #defined 为 0,并且在语义上将它与整数 0 区分开来非常有用。
There are problems with using 0 (and hence NULL). For example:
使用 0(因此为 NULL)存在问题。例如:
void f(int);
void f(void*);
f(0); // Ambiguous. Calls f(int).
The next version of C++ (C++0x) includes nullptr
to fix this.
下一版本的 C++ (C++0x) 包括nullptr
修复这个问题。
f(nullptr); // Calls f(void*).
回答by CB Bailey
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.
NULL
不是核心 C++ 语言的本机部分,但它是标准库的一部分。您需要包含包含其定义的标准头文件之一。#include <cstddef>
或者#include <stddef.h>
应该足够了。
The definition of NULL
is guaranteed to be available if you include cstddef
or stddef.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
如果包含cstddef
或,则保证的定义可用stddef.h
。不能保证,但如果您包含许多其他标准标头,则很可能会包含其定义。
回答by Andy White
Are you including "stdlib.h" or "cstdlib" in this file? NULL is defined in stdlib.h/cstdlib
您是否在此文件中包含“stdlib.h”或“cstdlib”?NULL 在 stdlib.h/cstdlib 中定义
#include <stdlib.h>
or
或者
#include <cstdlib> // This is preferrable for c++
回答by paxdiablo
Don't use NULL
, C++ allows you to use the unadorned 0
instead:
不要使用NULL
,C++ 允许您使用未修饰的0
代替:
previous = 0;
next = 0;
And, as at C++11, you generally shouldn't be using either NULL
or0
since it provides you with nullptr
of type std::nullptr_t
, which is better suited to the task.
并且,在 C++11 中,您通常不应该使用它们,NULL
或者0
因为它为您提供nullptr
了std::nullptr_t
更适合该任务的 type 。