131

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.

2
  • 2
    Perhaps you haven't declared NULL in the scope of where the message is coming from? Jan 20, 2009 at 17:08
  • 2
    You should atleast post the complete piece of code which is giving the error. Otherwise it will be very difficult to tell what is happening by just looking at the error string.
    – Naveen
    Jan 20, 2009 at 17:11

9 Answers 9

191

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.

5
  • 1
    God, I am so spoiled with Java (null being a keyword) that I never thought that NULL would not be a keyword in c++. Thanks :) Oct 4, 2011 at 19:45
  • 15
    Actually it's not a keyword in Java either. Jun 19, 2012 at 22:11
  • 4
    @ManofOneWay No, it exists in Java, it's just spelled with all lowercase instead of capitals. Mar 17, 2013 at 4:08
  • 8
    @ZettaSuro I didn't say it don't exist. I just said it isn't a keyword. Mar 19, 2013 at 19:44
  • 9
    Now that C++11 is more generally supported by compilers, it might be worth mentioning the nullptr keyword, which is an actual keyword and doesn't require any #includes. It's also more typesafe than NULL. Mar 10, 2015 at 1:10
39

GCC is taking steps towards C++11, which is probably why you now need to include cstddef in order to use the NULL constant. The preferred way in C++11 is to use the new nullptr keyword, which is implemented in GCC since version 4.6. nullptr is not implicitly convertible to integral types, so it can be used to disambiguate a call to a function which has been overloaded for both pointer and integral types:

void f(int x);
void f(void * ptr);

f(0);  // Passes int 0.
f(nullptr);  // Passes void * 0.
4
  • 1
    But still It is a strange behavior! Even compiling my code with -std=c++98 GCC stills don't recognize NULL macro, and It only recognize nullptr with either c++11 or gnu++11 as argument for -std.
    – pharaoh
    Nov 17, 2012 at 17:41
  • 2
    The C++ standard has stated already in 1998 that NULL is defined in cstddef - the new compiler versions just follow the standard more strictly because they need to implement nullptr. Your (faulty) code has compiled with earlier GCC versions, but it would be difficult to stay backward compatible with earlier GCC versions, in addition to earlier C++ standard versions. Nov 18, 2012 at 19:47
  • 1
    NULL has never been a built-in keyword; it's a macro defined in several standard C headers, including <stddef.h> (or <cstddef>). How does gcc "taking steps towards C++11" affect this? I see nothing in the question that implies that the (unseen) code compiled with earlier versions of gcc/g++, or with earlier versions of the language standard. Sep 23, 2013 at 17:58
  • 1
    That's just what I said above: Already in C++98 it was defined in cstddef. Still gcc (and other compilers too) accepted code that used NULL without including cstddef first. I'm quite sure also the (unseen) code in question compiled with earlier versions, even though it's not strictly standard compliant. Now I'm only guessing that this stricter behavior of modern versions is due to development of the compiler to support the C++11 syntax. Sep 24, 2013 at 19:18
10

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.

6

To complete the other answers: If you are using C++11, use nullptr, which is a keyword that means a void pointer pointing to null. (instead of NULL, which is not a pointer type)

2

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

#include <iostream>
1

NULL can also be found in:

#include <string.h>

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

0

Yes, the error really under certain circumstances can appear and disappear without changing the code as stated in the question: "It appears and disappears and I don't know why.".

In my case the same happened when compiling the code for ATmega328 using PlatformIO with Visual Studio Code.

For example, if there is main.cpp file which includes Arduino.h and a header file containing NULL then error 'NULL' was not declared in this scope will sometimes appear but sometimes will not and build will sometimes be without error but sometimes there will be error.

Sometimes the error 'NULL' was not declared in this scope will show alongside with the first error but not every time but then suddenly both errors might disappear and build might start working again.

In this case it was enough to copy #include <Arduino.h> from main.cpp to header file but the question is why build sometimes work even without including anything into the header file.

Seems as without including the definition at the top of the header file the compiler is not always resolving the symbols in the same order and sometimes the definition of NULL is resolved before parsing the code where it is used - but sometimes it isn't.

main.cpp

#include <Arduino.h>
#include <something.h>
.
.
.

something.h

.
.
.
Some_class *Ptr = NULL;

This code will sometimes generate the error but sometimes will not. At least when using VS Code + PlatformIO + target ATmega328.

-1

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...

1
  • NULL is defined in <cstddef> or <stddef.h>, and is required by the language standard. Don't define it yourself; just include a header that defines it. Mar 17 at 4:44
-1

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

#define NULL __null

So basically, you may use the __null keyword instead.

1
  • 1
    __null is not a standard keyword. Some compilers may have __null as an implementation-defined keyword and define NULL as __null, but you shouldn't rely on it. (And most standard headers don't define NULL, only <stddef.h> or <cstddef> and any other headers that include them.) Mar 17 at 4:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.