jackhab
jackhab

Reputation: 17718

error: ‘NULL’ was not declared in this scope

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.

Upvotes: 132

Views: 193289

Answers (8)

Chupo_cro
Chupo_cro

Reputation: 718

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.

Upvotes: 0

Vedant Panchal
Vedant Panchal

Reputation: 75

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

#define NULL __null

So basically, you may use the __null keyword instead.

Upvotes: -1

khaled mohamed ram
khaled mohamed ram

Reputation: 59

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

#include <iostream>

Upvotes: 2

Owl
Owl

Reputation: 1562

NULL can also be found in:

#include <string.h>

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

Upvotes: 1

David Thornley
David Thornley

Reputation: 57076

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.

Upvotes: 10

Leonardo Raele
Leonardo Raele

Reputation: 2823

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)

Upvotes: 6

Seppo Enarvi
Seppo Enarvi

Reputation: 3663

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.

Upvotes: 39

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507433

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.

Upvotes: 191

Related Questions