|
This thread is locked; no one can reply to it. |
1
2
|
'NULL' was not declared in this scope |
Sebastian Mineur
Member #6,943
February 2006
|
Hey, guys! 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
|
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. -- |
Sebastian Mineur
Member #6,943
February 2006
|
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. 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.S. In capitalist America bank robs you. |
James Stanley
Member #7,275
May 2006
|
#ifndef NULL #define NULL 0 #endif
|
GullRaDriel
Member #3,861
September 2003
|
Isn't NULL supposed to be 0 or (void *)0 ? Edited "Code is like shit - it only smells if it is not yours" |
James Stanley
Member #7,275
May 2006
|
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
|
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" |
James Stanley
Member #7,275
May 2006
|
You sure you haven't got those the wrong way round? |
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
|
Ha. Fair enough. |
Tobias Dammers
Member #2,604
August 2002
|
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. --- |
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. The face of a child can say it all, especially the mouth part of the face. |
Thomas Fjellstrom
Member #476
June 2000
|
Well, that and 0 is what you're SUPPOSED to use -- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Except that using NULL reminds you that you're talking about a pointer and not just an integer (even though pointers are basically integers anyway). My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Thomas Fjellstrom
Member #476
June 2000
|
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. -- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
It's not like you can't #ifdef #undef #define it if you want to. Also , did you read the part of Stroustrup's FAQ that Speedo quoted above? Aesthetic difference is what he called it. FAQ #null My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Vanneto
Member #8,643
May 2007
|
Just use 0. In capitalist America bank robs you. |
type568
Member #8,381
March 2007
|
Lend me a flamethrower somebody.
|
Arthur Kalliokoski
Second in Command
February 2005
|
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
|
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
|
Sorry... Just thought this was funny: www.nullptr.org ---- |
bamccaig
Member #7,536
July 2006
|
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? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
1
2
|