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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:59:32  来源:igfitidea点击:

Why is NULL undeclared?

c++syntaxnodes

提问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

NULLis 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 0instead, 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 NULLdefinition.

得到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 nullptrto fix this.

下一版本的 C++ (C++0x) 包括nullptr修复这个问题。

f(nullptr); // Calls f(void*).

回答by CB Bailey

NULLisn'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 NULLis guaranteed to be available if you include cstddefor 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 0instead:

不要使用NULL,C++ 允许您使用未修饰的0代替:

previous = 0;
next = 0;

And, as at C++11, you generally shouldn't be using either NULLor0since it provides you with nullptrof type std::nullptr_t, which is better suited to the task.

并且,在 C++11 中,您通常不应该使用它们,NULL或者0因为它为您提供nullptrstd::nullptr_t更适合该任务的 type 。