NULL Undeclared !?

Started by
15 comments, last by GameDev.net 18 years ago
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.
Advertisement
>How come NULL is undeclared !?

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

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
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.
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.
NULL is declared in alloc.h, mem.h, stddef.h, stdio.h and stdlib.h :)

Personally I'd include stddef.h :)
_______________The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
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>.
Doesn't really matter what standard header you include. Most of them define null or include something else that does.
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.

Stephen M. Webb
Professional Free Software Developer

Who cares? Only kids out of college use NULL. Real programmers use 0.

This topic is closed to new replies.

Advertisement