error: ‘NULL’ was not declared in this scope

Viewed 180892

I get this message when compiling C++ on gcc 4.3

error: ‘NULL’ was not declared in this scope

It appears and disappears and I don't know why. Why?

Thanks.

8 Answers

NULL is not a keyword. It's an identifier defined in some standard headers. You can include

#include <cstddef>

To have it in scope, including some other basics, like std::size_t.

NULL isn't a keyword; it's a macro substitution for 0, and comes in stddef.h or cstddef, I believe. You haven't #included an appropriate header file, so g++ sees NULL as a regular variable name, and you haven't declared it.

NULL can also be found in:

#include <string.h>

String.h will pull in the NULL from somewhere else.

NULL is not a keyword. It's an identifier defined in some standard headers. You can include

#include <iostream>

You can declare the macro NULL. Add that after your #includes:

#define NULL 0

or

#ifndef NULL
#define NULL 0
#endif

No ";" at the end of the instructions...

If you look carefully into NULL macro in any std header:

#define NULL __null

So basically, you may use the __null keyword instead.

Related