Thread: 'NULL' undeclared (first use this function)

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    'NULL' undeclared (first use this function)

    Is there a reason my compiler would say 'NULL' undeclared (first use this function) ?

    To my knowledge 'NULL' is not a function.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    add this
    Code:
    #define NULL 0

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    NULL gets defined in stdlib.h I believe.
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by King Mir View Post
    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.
    Can you elaborate on that a bit? I always use NULL for pointers.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    NULL and 0 are the same thing.
    NULL improves readability IMO and I also use it for pointers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    Has NULL ever been defined as anything else in any C or C++ standard?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sand_man View Post
    Has NULL ever been defined as anything else in any C or C++ standard?
    In C, NULL is defined as "an implementation-defined null pointer constant"; I think all the ones I've seen are cast to (void *).

  10. #10
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    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?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sirconnorstack View Post
    Is there a reason my compiler would say 'NULL' undeclared (first use this function) ?

    To my knowledge 'NULL' is not a function.
    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.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by cpjust View Post
    Can you elaborate on that a bit? I always use NULL for pointers.
    The ambiguity usually occurs when functions are overloaded to take either a pointer or an integer.

    For example:
    Code:
    string eg;
    eg+= NULL;
    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.
    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.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    There are other less contrived possibilities.

    Another situation that ambiguity occurs is with function template parameter deduction. For example:

    Code:
    make_pair(NULL,0);
    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.
    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.

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    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;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM