Is there a reason my compiler would say 'NULL' undeclared (first use this function) ?
To my knowledge 'NULL' is not a function.
Printable View
Is there a reason my compiler would say 'NULL' undeclared (first use this function) ?
To my knowledge 'NULL' is not a function.
add this
Code:#define NULL 0
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.
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:
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.
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
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;
But this is wrong use. You should never add NULL to something.
You set NULL to pointers you want to point nowhere, and not much else. If you do, the NULL loses its meaning!
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);
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;