为什么 NULL 未声明?

发布于 2024-07-21 22:24:11 字数 608 浏览 6 评论 0原文

当我尝试编译这段代码时,我遇到了这个结构构造函数的问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小ぇ时光︴ 2024-07-28 22:24:11

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 literal 0 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.

染年凉城似染瑾 2024-07-28 22:24:11

一定要使用 NULL。 无论如何,它只是 #define 为 0,并且在语义上将其与整数 0 区分开来非常有用。

使用 0(因此为 NULL)存在问题。 例如:

void f(int);
void f(void*);

f(0); // Ambiguous. Calls f(int).

C++ (C++0x) 的下一个版本包含 nullptr 来修复此问题。

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

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:

void f(int);
void f(void*);

f(0); // Ambiguous. Calls f(int).

The next version of C++ (C++0x) includes nullptr to fix this.

f(nullptr); // Calls f(void*).
猫烠⑼条掵仅有一顆心 2024-07-28 22:24:11

您在此文件中包含“stdlib.h”或“cstdlib”吗? NULL 在 stdlib.h/cstdlib 中定义

#include <stdlib.h>

#include <cstdlib>  // This is preferrable for c++

Are you including "stdlib.h" or "cstdlib" in this file? NULL is defined in stdlib.h/cstdlib

#include <stdlib.h>

or

#include <cstdlib>  // This is preferrable for c++
梦魇绽荼蘼 2024-07-28 22:24:11

NULL 不是核心 C++ 语言的本机部分,但它是标准库的一部分。 您需要包含包含其定义的标准头文件之一。 #include#include 应该足够了。

如果包含 cstddefstddef.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 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.

无声静候 2024-07-28 22:24:11

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

previous = 0;
next = 0;

并且,与 C++11 一样,您通常不应使用 NULL 0,因为它为您提供了 std::nullptr_t 类型的 nullptr,即更适合该任务。

Don't use NULL, C++ allows you to use the unadorned 0 instead:

previous = 0;
next = 0;

And, as at C++11, you generally shouldn't be using either NULL or 0 since it provides you with nullptr of type std::nullptr_t, which is better suited to the task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文