Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2006, 1:29 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
'NULL' was not declared in this scope

I keep getting the error "'NULL' was not declared in this scope" in all my C++ programs. From what I understand, 0 is the equivalent of NULL, but I am wondering why NULL does not seem to work.

I am working on a LinkedList class, and am having the problem that when I try to access the last ListNode in the list, the loop does not stop cycling through memory after it has reached the last ListNode. I am trying to compare the nodes to 0 (empty nodes have been initialized to 0 because NULL does not work). What am I doing wrong? Thanks.

int LinkedList::dequeue() {
    ListNode *current = front;
    while (current->next != 0) { // What should my test condition be?
        current = current->next;
    }
    return current->value;
}

EDIT: I solved the problem, I had not initialized the front ListNode to 0.

Last edited by titaniumdecoy; Mar 22nd, 2006 at 1:42 PM.
titaniumdecoy is online now   Reply With Quote
Old Mar 22nd, 2006, 1:45 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I still need help with using NULL instead of 0.

I have another question, as well: Why can I only initialize an object I have created with a 0 parameter constructor as follows:

LinkedList list;
And not

LinkedList list();
Since I have declared a 0 parameter constructor:

LinkedList::LinkedList()
  : front( 0 ) { }
titaniumdecoy is online now   Reply With Quote
Old Mar 22nd, 2006, 1:50 PM   #3
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Quote:
Originally Posted by titaniumdecoy
I have another question, as well: Why can I only initialize an object I have created with a 0 parameter constructor as follows:

LinkedList list;
And not

LinkedList list();
A constructor is automatically called on object initialization, that's why you don't have "list()". If a constructor wasn't automatically called, using the brackets would be necessary. If you require a constructor to get input, you could use brackets, like so:


LinkedList::LinkedList(int a)
{
    b = a; //b is a data member of LinkedList
}

LinkedList list(5); //set b to 5
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Mar 22nd, 2006, 1:55 PM   #4
bgeraghty
Newbie
 
Join Date: Mar 2006
Posts: 15
Rep Power: 0 bgeraghty is on a distinguished road
Well, in regards to your NULL situation, I can't tell you much without a little more info. Where were you trying to use NULL that caused this. One thing to keep in mind is that NULL is really a pointer to 0.

Paste the code where NULL was bugging out and maybe I can help.

Also, Which compiler do you use?
bgeraghty is offline   Reply With Quote
Old Mar 22nd, 2006, 2:04 PM   #5
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
http://www.eskimo.com/~scs/cclass/notes/sx10d.html
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Mar 22nd, 2006, 2:06 PM   #6
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
#include <cstdlib>

In cstdlib, NULL is defined.

EDIT: damn, jayme was 2 minutes faster
Polyphemus_ is offline   Reply With Quote
Old Mar 22nd, 2006, 2:36 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
NULL is really a pointer to 0.
The definition of the NULL pointer is much more complicated. On your system, with your compiler, perhaps its value is zero. Perhaps not. Please be careful with the answers you give novices.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 22nd, 2006, 5:39 PM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by DaWei
The definition of the NULL pointer is much more complicated. On your system, with your compiler, perhaps its value is zero. Perhaps not. Please be careful with the answers you give novices.
Ah yeah, remember the 7 page thread on this discussion on DS? I learned a lot from that, hope that grumpy makes an 'article' about that in the Contributor's Corner.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 22nd, 2006, 5:49 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The important thing to remember is that a pointer value of zero is required to be interpreted by the compiler as NULL, even if the actual value of NULL is not zero in some particular implementation, and that a dereferenced NULL pointer should not access valid memory. At least, that's what my feeble brain is dredging up, right now. We can hope that Grumpy weighs in here, and not the turkey that says, "pointers don't have values." The concept of the NULL pointer is so esoteric that I don't really cover it in great detail in my tutorial.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 24th, 2006, 3:00 AM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Hmmm.... I would have responded earlier, but I've been away due to a family emergency.

NULL is a macro that is specified in the C standard (declared in <stdlib.h>). In C++ it is supported in <cstdio> which might mean (i.e. I haven't checked the standard, so take this with a grain of salt) it is a constant in namespace std rather than a macro.

As Dawei said, if a pointer is initialised to zero the result is the same as if that pointer was assigned as the NULL pointer. Similarly, if a pointer is compared to zero (which includes tests like pointer == 0 or, more tersely, !pointer) the result is the same as if the pointer is compared to a NULL pointer of the same type (i.e. the test "pointer == 0" is the same as the test "pointer == NULL" or [more precisely] "pointer == (type_of_pointer *)NULL"). The NULL pointer is a special value of a pointer that effectively indicates "this pointer points at nothing".

Despite all this, there is no requirement that NULL be a zero value. The compiler is required to do a conversion of a zero to a NULL value behind the scenes when working with pointers. But the value of "0 == (int)NULL" is not required to give a true (or non-zero integer in C) result.

The declarations "LinkList list;" and "LinkedList list();" are equivalent in C++ if LinkedList has a default constructor (a constructor that accepts no arguments). In C++, naming a variable named list may confuse people (but not the compiler) as std::list is a template type in the standard library.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC