Sign in to follow this  
Cybernator

NULL Undeclared !?

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
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
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
<<Who cares? Only kids out of college use NULL. Real programmers use 0.>>

Blink. I avoid using any number where ever possible as 0 can mean true or false or success or failure or valid or invalid... Though I've been told I'm wierd before...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I typecast NULL as NOTHING and then define VOID, NADA, BLANK as NOTHING... just to make it even more confusing for noobs ;)


and btw: real programmers program in 1's and 0's so that they never have to deal with stupid name associations.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
visual studio 2005 professional has:

#define NULL (-1)

Share this post


Link to post
Share on other sites
It's either a very bad joke or a troll.

From stdlib.h (which is included by cstdlib):

/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif

Share this post


Link to post
Share on other sites
Thanks for the enlightenment, dudes! ;)

Looking back, it turns that it wasn't strange at all. I'd say never listen to your lecturer.
When I first started learning C, I guessed NULL was declared/defined in "stdio.h". But NULL was being used just about everywhere, in every book, in every tutorial, whether it was DOS or Windows. So I thought "windows.h" was either including something from the standard libs, or it declares NULL on its own. But I never excluded the possibility that NULL could be "internally declared/defined". Whatever... The thing is that NULL has to be some value, and what would it be if not 0?

Sometime later I get to learn this at university, and the lecturer says: "NULL is not the same as 0". (Or at least I misunderstood). So if it's not the same, than it must be internally declared, so the compiler knows it as a pointer.

And now, for the first time in 4 years I get this "strange" error message. ;) Apparently I REM'ed out the include directive to see if I can kick the error messages regarding the template. Off to download the newer version of VC. Hopefully it doesn't require Windows XP.

Thanks again!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Maybe your lecturer meant the NULL in C and in C it's not necessarily 0 oder (void*)0. The value is implementation defined.
But in c++ 0 and NULL are the same.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this