Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » 'NULL' was not declared in this scope

This thread is locked; no one can reply to it. rss feed Print
 1   2 
'NULL' was not declared in this scope
Sebastian Mineur
Member #6,943
February 2006
avatar

Hey, guys!
Can anyone tell me why the compiler would give me this error with the following code:

VECTOR* MODEL::create_vertice(float fPosX, float fPosY)
{
    if(this->iNumberOfVertices < 100)
    {
        this->Vertices[this->iNumberOfVertices] = new VECTOR(fPosX, fPosY);
        return this->Vertices[this->iNumberOfVertices++];
    }
    return NULL;
}

NULL is a standard part of the C/C++ language, and shouldn't have to be declared, right? And it's wierd 'cause it compiles this code just fine:

MODEL* VGFX::create_model()
{
    if(this->iNumberOfModels < 100)
    {
        this->Models[this->iNumberOfModels] = new MODEL;
        return this->Models[this->iNumberOfModels++];
    }
    return NULL;
}

To me, the the two snippets are using the keyword in exactly the same way...

X-G
Member #856
December 2000
avatar

Quote:

NULL is a standard part of the C/C++ language, and shouldn't have to be declared, right?

Neg. Allegro #defines it though, which is probably why it works in the other file.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Sebastian Mineur
Member #6,943
February 2006
avatar

Quote:

Neg. Allegro #defines it though, which is probably why it works in the other file.

Hmmm, I see. And you're absolutely right! VGFX included allegro.h, but MODEL did not.
Thanks for the help, and for the quick reply :)

Oh, and prepare for the revolution. The FRA-law is completely unacceptable!

Speedo
Member #9,783
May 2008

You still have to include a definition of NULL somewhere. For example this won't compile:

int main( ) {
  int * foo = NULL;
  return 0;
}

but this will:

#include <stdlib.h>

int main( ) {
  int * foo = NULL;
  return 0;
}

Vanneto
Member #8,643
May 2007

Or use 0 and stop worrying about it. :P

P.S.
I'm aware that C's definition of NULL is (void*)0. But he said C/C++, which means he sometimes writes C code using a C++ compiler. His code also leads me to believe he uses C++. So 0 it is. \o/

In capitalist America bank robs you.

James Stanley
Member #7,275
May 2006
avatar

#ifndef NULL
#define NULL 0
#endif

GullRaDriel
Member #3,861
September 2003
avatar

Isn't NULL supposed to be 0 or (void *)0 ?

Edited

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

James Stanley
Member #7,275
May 2006
avatar

Makes no difference to me. C doesn't care. And seeing as the OP has been instructed to use <stdlib.h> and not <cstdlib>, and there was no correction about C++ not C, I think it's fair to say he's using C.

GullRaDriel
Member #3,861
September 2003
avatar

I have just found this:

#define NULL 0;                   // A typical definition of NULL in C++ 
#define NULL  ((void*)0)          // C defines NULL this way

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

James Stanley
Member #7,275
May 2006
avatar

You sure you haven't got those the wrong way round?
In my experience, C++ complains about not casting, while C doesn't care.

Vanneto
Member #8,643
May 2007

He is correct.

// C++ 
#ifndef NULL
    #define NULL 0
#endif

// C
#ifndef NULL
    #define NULL (void*)0
#endif

In capitalist America bank robs you.

James Stanley
Member #7,275
May 2006
avatar

Ha. Fair enough.
I tried to look in my computer, but stdlib.h said it gets it from stddef.h, and stddef.h isn't known to whereis.

Tobias Dammers
Member #2,604
August 2002
avatar

Stroustup himself recommends using 0 instead of NULL, which is completely counter-intuitive (seeing that 0 is an integer, not a pointer), but that's how He sees it. Last time I checked, that is.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Speedo
Member #9,783
May 2008

Quote:

Makes no difference to me. C doesn't care. And seeing as the OP has been instructed to use <stdlib.h> and not <cstdlib>, and there was no correction about C++ not C, I think it's fair to say he's using C.

Well, I pointed him to stdlib.h because, at least in MSVC, that's where NULL is actually defined (cstdlib includes stdlib.h):

/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif

Edit: I got myself absorbed in reading some of Stroustrup's C++ FAQs, and happened to come across what he says about NULL:

Quote:

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.

If you have to name the null pointer, call it nullptr; that's what it's going to be called in C++0x. Then, "nullptr" will be a keyword.

Jonny Cook
Member #4,055
November 2003

I usually use 0 instead of NULL. Makes my source files smaller. ;D

The face of a child can say it all, especially the mouth part of the face.

Thomas Fjellstrom
Member #476
June 2000
avatar

Well, that and 0 is what you're SUPPOSED to use ;)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Thomas Fjellstrom
Member #476
June 2000
avatar

Except in C++ NULL may be declared as (void *)0 which is completely incompatible with any other type in the known C++ universe, and the compiler will barf all over you. Sure, some headers might "fix" that for you, it might not be fixed everywhere. Also, go ask C++'s creator if its a recommended thing to do.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Vanneto
Member #8,643
May 2007

Just use 0.

In capitalist America bank robs you.

type568
Member #8,381
March 2007
avatar

Lend me a flamethrower somebody.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Pointers are integers, although the default size of integer may be different from the default size of pointers. Kind of like an index for all of memory rather than just an array.

They all watch too much MSNBC... they get ideas.

BAF
Member #2,981
December 2002
avatar

Quote:

If you have to name the null pointer, call it nullptr; that's what it's going to be called in C++0x. Then, "nullptr" will be a keyword.

So C++0x isn't going to make null actually null (instead of 0 or some other crap)? That's lame.

Ron Novy
Member #6,982
March 2006
avatar

Sorry... Just thought this was funny: www.nullptr.org

----
Oh... Bieber! I thought everyone was chanting Beaver... Now it doesn't make any sense at all. :-/

bamccaig
Member #7,536
July 2006
avatar

BAF said:

So C++0x isn't going to make null actually null (instead of 0 or some other crap)? That's lame.

What's the difference? ::) And how do you expect to efficiently represent nullptr as anything other than 0? :-/

 1   2 


Go to: