tags:

views:

846

answers:

5

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

+2  A: 

Are you including "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++
Andy White
+9  A: 

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.

Otherwise, add

#include <stddef.h>

to get the NULL definition.

UPDATE: I had the wrong header, corrected now. Thanks!

unwind
NULL is part of stddef.h, not stdlib.h. Technically, you aren't guaranteed to get it as part of stdlib.h although I admit it would be pretty suprising if you didn't.
Charles Bailey
NULL is defined in the following C headers: stddef.h, stdlib.h, stdio.h, locale.h, string.h, and time.h (and wchar.h if you count C99).
Michael Burr
OK, I stand corrected. I wonder why C++ needs to mention the contents of cstddef explicitly, and not the other headers. Perhaps it's considered the canonical 'C++' place for NULL whereas the other locations are for C compatibility?
Charles Bailey
@Charles Bailey C++ mentions cstddef explicitly because it defines NULL to 0 and not to ((void *)0) as in C99. This is because in C++ implicit casts from void * are forbidden.
lothar
Oh and in the upcoming C++0x there will be a new keyword null_ptr. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2105.html
lothar
I know why the standard needs to talk about the type of NULL, I just wonder why it only mentions one the places that its defined in the C standard library. At the moment, the new keywordin C++0x is still spelled 'nullptr'.
Charles Bailey
NULL is defined in quite a lot of C headers - and <stdlib.h> is one of those headers (C99 for sure; I don't think it changed between C89 and C99). That said, <stddef.h> is the smallest header to use if you aren't using anything else from <stdlib.h>. Also, given that this is C++, should that be <cstddef>?
Jonathan Leffler
>> Perhaps it's considered the canonical 'C++' place for NULL << I think this is correct. In the C standard, wherever it's mentioned that NULL is defined in a header other than stddef.h, there's always a note to see the description of NULL for stddef.h.
Michael Burr
Richard Corden
+5  A: 

NULL isn't 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 sufficient.

The definition of NULL is guaranteed to be available if you include cstddef or stddef.h. It's not guaranteed, but you are very likely to get its definition included if you include many of the other standard headers instead.

Charles Bailey
+2  A: 

Don't use NULL, C++ allows you to use the unadorned 0 instead:

previous = 0;
next = 0;
paxdiablo
I tend think that NULL is useful documentation that you are intending to use a null pointer constant rather than an integer constant, although I don't object to using 0. I'll admit that you don't gain any practical benefits at the moment, but if/when you adopt the next C++ version it gives a good start for places to change to use the new nullptr constant.
Charles Bailey
i agree with you both, of course. Incidentally, it's both good that one documents one uses a pointer, but also good one documents that one actually puts an integer forward. consider printf("%p\n", NULL); // OH, UB. Or if you have two overloads, void f(int); void f(void*); you might think that f(NULL); calls the void* version when having a quick look on the call. f(0); will document the fact that it will actually call the int version, but won't document the fact that you intend you pass a pointer :( Good that nullptr fixes it :)
Johannes Schaub - litb
+4  A: 

Do use NULL. It is just #defined as 0 anyway and it is very useful to semantically distinguish it from the integer 0.

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

void f(int); void f(void*);

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

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

f(nullptr); // Calls f(void*).