set head to NULL ('NULL' : undeclared identifier)
Solution 1
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.)
Solution 2
You should include <stddef.h>
or <cstddef>
.
However you can use 0
or nullptr
too.
Solution 3
use the following include:
#include <stddef.h>
Solution 4
No libraries needed!
on the top of the header file,
do this:
#ifndef NULL
#define NULL (0)
#endif
Solution 5
NULL
isn't actually part of the core C or C++ language; it's defined to be 0 in stddef.h
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)
Related videos on Youtube
Alon Shmiel
Updated on July 09, 2022Comments
-
Alon Shmiel almost 2 years
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:movie.h(40): error C2065: 'NULL' : undeclared identifier
please note that I can't include any library except of
iostream
Any help appreciated!
-
Nick about 11 yearsThat's because
NULL
hasn't been defined. Either define it or sethead = 0
-
CassOnMars about 11 yearsWhy did you delete your code?
-
Alon Shmiel about 11 yearsbecause there are students that can copy my code..
-
Chowlett about 11 yearsThey still can; it's in the edit history.
-
metal about 11 yearsWoe to those students who copy broken code! :-P
-
-
Nick about 11 yearsI think
nullptr
is only for c++11 -
Aniket Inge about 11 yearsincluding a header file for a macro is unnecessary. I am not the downvoter though
-
Aniket Inge about 11 yearsOP is not allowed to include headers!
-
Aniket Inge about 11 yearsOp is not allowed to include headers. and assigning a pointer to an integer will cause compiler error.
-
metal about 11 yearsNo it won't. Try
int* p = 0;
Very common. -
Aniket Inge about 11 yearshmm @metal try this:
int *p = 0x1
- however the compiler says it is ok to assign 0 but not 1 -
James Kanze about 11 years@Aniket The only way to get
NULL
is by including one of the standard headers which define it. -
Aniket Inge about 11 yearsthat's certainly not true @JamesKanze the other way is posted below as my answer
-
metal about 11 yearsThus spake the Creator: stroustrup.com/C++11FAQ.html#nullptr Note also the references.
-
James Kanze about 11 years@Aniket That's because a constant integral expression evaluating to 0 is a null pointer constant, and converts implicitly to a null pointer of any type. How did you think
NULL
was defined? -
James Kanze about 11 years@Aniket He has to, otherwise he cannot use
NULL
. -
James Kanze about 11 yearsThis is forbidden by the standard, and results in undefined behavior.
-
James Kanze about 11 years@Aniket Your answer is wrong. It's illegal to define
NULL
in your code (at least if you include any standard headers). -
James Kanze about 11 yearsWorse: the definition you give isn't even legal.
-
Aniket Inge about 11 years@JamesKanze it is legal now
-
Aniket Inge about 11 yearsThe problem was the difference between
(void *)0
being NULL in C and0
in C++ because of a certain incompatibilities. -
James Kanze about 11 yearsProvided it is in one of the standard headers. You're not allowed to provide definitions of things that are in the standard headers.
-
Aniket Inge about 11 yearsbut he is not including standard headers. And you can always undefine any macro(even in standard libraries)
-
James Kanze about 11 yearsThere's no real difference between traditional C and C++ here.
NULL
has always been0
, since the days of Kernighan and Richie. For some unknown reason, the C standards committee decided to allow an additional form, but it wasn't in K&R C, and certainly isn't traditional. -
Aniket Inge about 11 years
-
James Kanze about 11 years
<iostream>
is a standard header. And while it is legal to undefineNULL
, it's not legal for you to define it. -
James Kanze about 11 yearsSee §17.6.4.3.1/1: "A translation unit that includes a standard library header shall not
#define
or#undef
names declared in any standard library header." -
metal about 11 yearsA proscription routinely violated, alas, when it comes to the min/max macros.