set head to NULL ('NULL' : undeclared identifier)

49,028

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)

Share:
49,028

Related videos on Youtube

Alon Shmiel
Author by

Alon Shmiel

Updated on July 09, 2022

Comments

  • Alon Shmiel
    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
      Nick about 11 years
      That's because NULL hasn't been defined. Either define it or set head = 0
    • CassOnMars
      CassOnMars about 11 years
      Why did you delete your code?
    • Alon Shmiel
      Alon Shmiel about 11 years
      because there are students that can copy my code..
    • Chowlett
      Chowlett about 11 years
      They still can; it's in the edit history.
    • metal
      metal about 11 years
      Woe to those students who copy broken code! :-P
  • Nick
    Nick about 11 years
    I think nullptr is only for c++11
  • Aniket Inge
    Aniket Inge about 11 years
    including a header file for a macro is unnecessary. I am not the downvoter though
  • Aniket Inge
    Aniket Inge about 11 years
    OP is not allowed to include headers!
  • Aniket Inge
    Aniket Inge about 11 years
    Op is not allowed to include headers. and assigning a pointer to an integer will cause compiler error.
  • metal
    metal about 11 years
    No it won't. Try int* p = 0; Very common.
  • Aniket Inge
    Aniket Inge about 11 years
    hmm @metal try this: int *p = 0x1 - however the compiler says it is ok to assign 0 but not 1
  • James Kanze
    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
    Aniket Inge about 11 years
    that's certainly not true @JamesKanze the other way is posted below as my answer
  • metal
    metal about 11 years
    Thus spake the Creator: stroustrup.com/C++11FAQ.html#nullptr Note also the references.
  • James Kanze
    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
    James Kanze about 11 years
    @Aniket He has to, otherwise he cannot use NULL.
  • James Kanze
    James Kanze about 11 years
    This is forbidden by the standard, and results in undefined behavior.
  • James Kanze
    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
    James Kanze about 11 years
    Worse: the definition you give isn't even legal.
  • Aniket Inge
    Aniket Inge about 11 years
    @JamesKanze it is legal now
  • Aniket Inge
    Aniket Inge about 11 years
    The problem was the difference between (void *)0 being NULL in C and 0 in C++ because of a certain incompatibilities.
  • James Kanze
    James Kanze about 11 years
    Provided 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
    Aniket Inge about 11 years
    but he is not including standard headers. And you can always undefine any macro(even in standard libraries)
  • James Kanze
    James Kanze about 11 years
    There's no real difference between traditional C and C++ here. NULL has always been 0, 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
    Aniket Inge about 11 years
  • James Kanze
    James Kanze about 11 years
    <iostream> is a standard header. And while it is legal to undefine NULL, it's not legal for you to define it.
  • James Kanze
    James Kanze about 11 years
    See §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
    metal about 11 years
    A proscription routinely violated, alas, when it comes to the min/max macros.