Jump to content
  • Advertisement
Sign in to follow this  
Cybernator

NULL Undeclared !?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I've been playing around with templates, and I've found something really strange. Check this out: <--- cut here ---> template <class T> class CLinkedList { protected: CLinkedList<T> *link; T *data; public: CLinkedList(); ~CLinkedList(); }; template <class T> CLinkedList<T>::CLinkedList() { link=NULL; data=NULL; } template <class T> CLinkedList<T>::~CLinkedList() { } <--- cut here ---> When I compile this with Visual C++ 6, I get the following errors: linkedlist.h(27) : error C2065: 'NULL' : undeclared identifier main.cpp(27) : while compiling class-template member function '__thiscall CLinkedList<int>::CLinkedList<int>(void)' linkedlist.h(27) : error C2440: '=' : cannot convert from 'int' to 'class CLinkedList<int> *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast main.cpp(27) : while compiling class-template member function '__thiscall CLinkedList<int>::CLinkedList<int>(void)' linkedlist.h(28) : error C2440: '=' : cannot convert from 'int' to 'int *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast main.cpp(27) : while compiling class-template member function '__thiscall CLinkedList<int>::CLinkedList<int>(void)' How come NULL is undeclared !? "Ok, so VC6 is buggy. Let's try DJGPP." DJGPP returns only one error. It says NULL is undeclared. 8-| I'd appreciate if someone can share some insight. Btw, this "LinkedList" doesn't operate.. Yet. ;) Also, is it ok to declare a pointer to the next node like this: CLinkedList<T> *link; ??? If it matters, I only instantiate an object of CLinkedList in main.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
>How come NULL is undeclared !?

Well, obviously nobody defined nor declared NULL. It's not a keyword.

Share this post


Link to post
Share on other sites

NULL is defined for example in string.h.

NULL isn't a compiler keyword just a #define NULL 0L (or something similar).

Some people even say that using NULL is bad, although I find it a bit clearer.

Cheers

Share this post


Link to post
Share on other sites
You could just do

#ifndef NULL
#define NULL 0
#endif

then your code won't break if you put a header in later that defines it first.

I thought it was okay to just use 0 in C++ though. I always do.

Share this post


Link to post
Share on other sites
Just a tip, get yourself the Visual C++ 2003 Toolkit (pluggable into VC6) or Visual C++ 2005 Express, both free downloads at the microsoft site.

Visual C++ 6 is OLD, BUGGY and so NON STANDARDS COMPLIANT that I can't call it C++ compiler anymore without blushing.

Share this post


Link to post
Share on other sites
NULL is declared in alloc.h, mem.h, stddef.h, stdio.h and stdlib.h :)

Personally I'd include stddef.h :)

Share this post


Link to post
Share on other sites
Just keep in mind that all the .h headers are C headers, e.g. it is not C++ you are writing, but some pre-standard form of it. The C++'ized C headers are prefixed with 'c' and don't have a '.h' suffix, e.g. <cstddef> instead of <stddef.h>.

Share this post


Link to post
Share on other sites
Doesn't really matter what standard header you include. Most of them define null or include something else that does.

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
Doesn't really matter what standard header you include. Most of them define null or include something else that does.


It depends.

If you want to write code that complies with the C++ standard as defined in the ISO documents (eg. ISO/IEC 14882:2003(E)) so that it is portable and uses strictly defined behaviour, thus minimizing both your development costs and porting/maintenance costs, you would include the header that is specified for whatever symbol you need (eg. for NULL you must #include <cstdlib>, and NULL must be a preprocessor macro that resolves to an rvalue of type pointer to p, yadda yadda yadda).

If all you want to do is get things working on whatever version of tool supplied by a particular vendor on the machine in front of you at the moment, then no it doesn't matter as long as it compiles. Heck, you don't even have to run your software to test it, as long as it compiles.

You can just choose to strive for prefessional quality. The professionals will curse you less when they are forced to fix your code if you do.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Who cares? Only kids out of college use NULL. Real programmers use 0.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!