# WTF??!! NULL is not defined in MSVC60??!!

This topic is 5062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I tried to compile a system of some nested classes and what happenned.. (small code dump:)

// in header file of the class

class gravityobjectsystem:public spaceobject
{
spaceobject* soa;
int nso;
public:
gravityobjectsystem();
virtual ~gravityobjectsystem();

///...........
// and then in .cpp file ...

gravityobjectsystem::gravityobjectsystem()  //constructor
{
nso=0;
soa=NULL;
}
//....

I thought that this sholdn't be a problem, but MSVC6.0 came with two errors: error C2065: 'NULL' : undeclared identifier //??????!!!!!! error C2440: '=' : cannot convert from 'int' to 'class spaceobject *' does somebody had any similar problem? I though NULL is valid zero pointer really EVERYWHERE? ISN'T??!! WTF?!!! helpme... (i don't want to #define my own NULL) exa_einstein

##### Share on other sites
Just use 0 (zero). Seriously

##### Share on other sites
I know this, but question here is a little more about that "Has the NULL disappeared?" bug in visualC? (I have really checked everything)

ok,ok. now I use soa=(spaceobject*)0; because soa doesn't look like normal integer with it...

##### Share on other sites
NULL is #defined or whatnot in various library headers. If you don't include any of these in your project, then NULL is just an undeclared identifier. If you really want you can always add
#ifndef NULL#define NULL 0#endif
to your code, although switching to simply using 0 might be just as good. Only a few rare systems using something other than 0 for NULL. I'd switch, but I'm too lazy at the moment. Actually, if it's a big enough project, I'd probably just make my own const variable that matched the naming convention of the rest of my constants, and was in the proper namespace, too, if I was using namespaces.

##### Share on other sites
class Null{	public:		template <typename TYPE>		operator TYPE*() const		{			return 0;		}};const Null null;GravityObjectSystem::GravityObjectSystem(){	aDescriptiveName = 0;	anotherDescriptiveName = null;}

Simple, clean, unambiguous.

Enigma

##### Share on other sites
Quote:
 Original post by Enigma *** Source Snippet Removed ***Simple, clean, unambiguous.Enigma

nice

##### Share on other sites
Actually, you'll have to define your own NULL - because it's normal definition place is in the windows.h headers (or one hanging off that).

Enigma: Sweet [wink]

##### Share on other sites
If I'm not mistaken, stddef.h always defines it. Of course, as Namethatnobodyelsetook mentioned, 0 is the correct C++ equivalent.

##### Share on other sites
Then why is there the line:

#define NULL ((void *)0)

in windows.h?

##### Share on other sites
Hi,

Quote:
 Original post by exa_einsteinI thought that this sholdn't be a problem, but MSVC6.0 came with two errors:error C2065: 'NULL' : undeclared identifier //??????!!!!!!error C2440: '=' : cannot convert from 'int' to 'class spaceobject *'does somebody had any similar problem? I though NULL is valid zero pointer really EVERYWHERE? ISN'T??!! WTF?!!!

You thought wrong. NULL is *not* defined everywhere. One of these days, try to compile the following code:

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

I just did, and GCC gave me the following:

test.c: In function main':test.c:2: NULL' undeclared (first use in this function)test.c:2: (Each undeclared identifier is reported only oncetest.c:2: for each function it appears in.)

That is because NULL is not a symbol defined by default, but is contained in stdlib.h. (And also happens to be defined in windows.h, and some other headers.)

Vovan

• 32
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999483
×