Is there a reason my compiler would say 'NULL' undeclared (first use this function) ?
To my knowledge 'NULL' is not a function.
Is there a reason my compiler would say 'NULL' undeclared (first use this function) ?
To my knowledge 'NULL' is not a function.
NULL gets defined in stdlib.h I believe.
NULL is defined in <cstdlib> or <cstddef>. You need to include them in the file.
Tough, since this is C++ I recommend using 0 instead of null. This will make ambiguity errors arising from the value being treated as an integer more obvious.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
NULL and 0 are the same thing.
NULL improves readability IMO and I also use it for pointers.
I tend to use NULL for C++ specific code, with Stroustrup's reasoning:
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Has NULL ever been defined as anything else in any C or C++ standard?
But Laserlight, Stroustrup seems to say that he prefers using '0' instead of 'NULL' for it is a macro, while you are using this argument to use NULL instead of 0?
I think you are misunderstanding the error message: First use in this function means that it is only recorded as undeclared ONCE, not every time it's being used (because it's usually only ONE mistake to not declare something - and the compiler telling you that 35 times because that's how many times the variable occurs in the code isn't really going to help).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The ambiguity usually occurs when functions are overloaded to take either a pointer or an integer.
For example:
Now in this case adding null to a sting does not make sense. But the result is not what you'd expect. string has 3 operator+=, one for strings, one for char *, and one for chars. I think the compiler will complain of ambiguity, although it might just treat NULL as a char.Code:string eg; eg+= NULL;
Last edited by King Mir; 06-04-2008 at 02:37 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
There are other less contrived possibilities.
Another situation that ambiguity occurs is with function template parameter deduction. For example:
Again I'm not sure if that would give an error or create a pair of integers. Either way, the mistake is not obvious unless 0 is used in place of NULL.Code:make_pair(NULL,0);
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
I use null to make the code more readable. When I see 0, I think "an int", when I see NULL, I think "a pointer".
Like so:
HWND hwnd = NULL;
long loop = 0;