为什么未声明 NULL?

2023-09-27C/C++开发问题
2

本文介绍了为什么未声明 NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

当我尝试编译此代码时,此结构构造函数出现问题:

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:

linkedlistlinkedlist.h||In constructor `Node::Node(int)':|
linkedlistlinkedlist.h|9|error: `NULL' was not declared in this scope|
    ||=== Build finished: 1 errors, 0 warnings ===|

最后一个问题是结构,但它在我的 main.cpp 中工作正常,这次它在头文件中并给了我这个问题.我正在使用 Code::Blocks 来编译这段代码

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

推荐答案

NULL 不是 C 或 C++ 语言中的内置常量.事实上,在 C++ 中它或多或少已经过时了,只需使用纯文字 0 代替,编译器会根据上下文做正确的事情.

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.

在较新的 C++(C++11 及更高版本)中,使用 nullptr(如评论中指出的,谢谢).

In newer C++ (C++11 and higher), use nullptr (as pointed out in a comment, thanks).

否则,添加

#include stddef.h>

获取NULL定义.

这篇关于为什么未声明 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6