C++ 将 head 设置为 NULL ('NULL' : 未声明的标识符)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15549873/
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
set head to NULL ('NULL' : undeclared identifier)
提问by Alon Shmiel
I defined a linked list in C++. I am trying to set a NULL value to the variable head (in the constructor of Movie_LinkedList
), but I got:
我在 C++ 中定义了一个链表。我试图为变量 head(在 的构造函数中Movie_LinkedList
)设置一个 NULL 值,但我得到:
movie.h(40): error C2065: 'NULL' : undeclared identifier
电影.h(40):错误C2065:'NULL':未声明的标识符
please note that I can't include any library except of iostream
请注意,除了iostream
Any help appreciated!
任何帮助表示赞赏!
回答by metal
As written, NULL
isn't defined in your program. Usually, that's defined in a standard header file -- specifically <cstddef>
or <stddef.h>
. Since you're restricted to iostream
, if yours doesn't get NULL
implicitly from that header, you can use 0
or, in C++11, nullptr
, which is a keyword and doesn't require a header. (It is not recommended to define NULL
yourself. It might work sometimes, but it is technically illegal.)
如所写,NULL
未在您的程序中定义。通常,在一个标准的头文件的定义-特异性<cstddef>
或<stddef.h>
。由于您仅限于iostream
,如果您没有NULL
从该标头中隐式获取,则可以使用0
or,在 C++11 中nullptr
,它是一个关键字并且不需要标头。(不建议定义NULL
自己。它有时可能有效,但在技术上是非法的。)
回答by masoud
You should include <stddef.h>
or <cstddef>
.
您应该包括<stddef.h>
或<cstddef>
。
However you can use 0
or nullptr
too.
但是,您也可以使用0
或nullptr
。
回答by Aniket Inge
No libraries needed!
不需要图书馆!
on the top of the header file,
在头文件的顶部,
do this:
做这个:
#ifndef NULL
#define NULL (0)
#endif
回答by ryrich
use the following include:
使用以下包括:
#include <stddef.h>
回答by Chowlett
NULL
isn't actually part of the core C or C++ language; it's defined to be 0 in stddef.h
NULL
实际上不是核心 C 或 C++ 语言的一部分;它在 stddef.h 中定义为 0
Since you're using C++, prefer nullptr
, which is a keyword, and typed. (Assuming it's available. It's part of C++11, so technically not all compilers will support it; practically, you'd be hard-pressed to find a compiler that doesn't)
由于您使用的是 C++,因此更喜欢nullptr
,这是一个关键字,并键入。(假设它可用。它是 C++11 的一部分,因此从技术上讲并非所有编译器都支持它;实际上,您很难找到不支持的编译器)