Why is NULL undeclared?

I have a problem with this structure constructor 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 find this error:

 \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 ===| 

The last problem was the structure, but it worked fine when it was in my main.cpp, this time it is in the header file and gives me this problem. I am using Code :: Blocks to compile this code

+66
c ++ syntax nodes
May 29 '09 at 6:30 a.m. a.m.
source share
5 answers

NULL not a built-in constant in C or C ++. In fact, in C ++ it is more or less deprecated, just use a simple literal 0 , and the compiler will do the right thing depending on the context.

In the new C ++ (C ++ 11 and above) use nullptr (as indicated in the comment, thanks).

Otherwise add

#include < stddef.h >

to get a NULL definition.

+109
May 29 '09 at 6:34 am
source share

Use NULL. It's just #defined as 0 anyway, and it is very useful to semantically distinguish it from an integer 0.

There are problems using 0 (and therefore NULL). For example:

 void f(int); void f(void*); f(0); // Ambiguous. Calls f(int). 

The next version of C ++ (C ++ 0x) includes nullptr to fix this.

 f(nullptr); // Calls f(void*). 
+31
May 29 '09 at 12:52
source share

NULL not a native part of the core C ++ language, but it is part of the standard library. You need to include one of the standard header files that include its definition. #include <cstddef> or #include <stddef.h> should be enough.

NULL guaranteed if you include cstddef or stddef.h . This is not guaranteed, but you are likely to include its definition if you include many other standard headers instead.

+14
May 29 '09 at 6:34 a.m.
source share

Do you include "stdlib.h" or "cstdlib" in this file? NULL is defined in stdlib.h / cstdlib

 #include <stdlib.h> 

or

 #include <cstdlib> // This is preferrable for c++ 
+9
May 29 '09 at 6:34 am
source share

Do not use NULL , C ++ allows you to use unvarnished 0 instead:

 previous = 0; next = 0; 

And, as in C ++ 11, you usually should not use NULL or 0 , since it provides you with nullptr type std::nullptr_t , which is better suited for the task.

+4
May 29 '09 at 6:38
source share



All Articles