Thread: setting equal to NULL

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question setting equal to NULL

    When I write this code, I get an error from my compiler saying that NULL is an undeclared identifier.

    Code:
    #ifndef DISK_H
    #define DISK_H
    
    class Disk
    {
    public:
    	Disk(int no) : number(no), next(NULL) {}
    	~Disk() {}
    
    	bool operator <(const Disk& rhs);
    	bool operator >(const Disk& rhs);
    	bool operator ==(int rhs);
    
    	int reveal_number()
    	{
    		return this->number;
    	}
    
    private:
    	int number;
    	Disk* next;
    };
    
    #endif
    I've also tried setting next equal to null inside the curly brackets instead, but that has the same problem. What's the problem?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    Thx. I'm still curious as to how it goes wrong though.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    NULL is not a keyword, it is a symbol that is defined somewhere. At the point when the compiler is compiling your code, it has not seen that definition (or rather, the pre-compiler has not replaced NULL with 0 because it hasn't seen the definition).

    NULL is defined according to the standard in a specific header (I forget which one), and on some platforms in some cases it tends to work even if you don't include that specific header. You just happen to have hit a situation where it doesn't work.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm still curious as to how it goes wrong though.
    If you really want to use NULL instead of 0, you should #include <cstddef>
    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

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    100
    Thank y'all. As for what y'all said about needing to pull out a header for it to not be a problem, usually I will include iostream and probably string, and it appears that iostream has definition of it as well as cstddef. thx.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In some (or even many) cases that might be true, but usually that is something you don't want to rely on because if you ever move your code to another platform (or post it for somebody else to try) then it suddenly won't work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM
  4. Having some trouble setting up Dev-c++ for DX
    By Iamien in forum Game Programming
    Replies: 7
    Last Post: 08-25-2003, 07:30 PM