为什么NULL未声明? [英] Why is NULL undeclared?
问题描述
我在尝试编译此代码时遇到此问题:
typedef struct Node
{
Node(int data)//
{
this-> data = data;
previous = NULL; //编译器在这里指示
next = NULL;
}
int data;
Node * previous;
Node * next;
} NODE;
当我遇到此错误时:
\linkedlist\linkedlist.h ||在构造函数`Node :: Node(int)':
\linkedlist\linkedlist.h | 9 |错误:`NULL'未在此范围中声明|
|| ===构建完成:1错误,0警告=== |
最后一个问题是struct,但是它在我的main.cpp中工作得很好,它在一个头文件,并给我这个问题。我使用Code :: Blocks编译此代码
NULL
不是C或C ++语言中的内置常量。事实上,在C ++中它或多或少过时,只是使用一个简单的文本 0
,而不是根据上下文编译器会做正确的事情。
在新的C ++(C ++ 11及更高版本)中,使用 nullptr
(如注释中指出, >
否则,添加
#include< stddef.h >
以获取 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:
\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
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.
这篇关于为什么NULL未声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!