![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Expert Programmer
|
'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. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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; LinkedList list(); LinkedList::LinkedList()
: front( 0 ) { } |
|
|
|
|
|
#3 | ||
|
Professional Programmer
|
Quote:
LinkedList::LinkedList(int a)
{
b = a; //b is a data member of LinkedList
}
LinkedList list(5); //set b to 5
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
||
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2006
Posts: 15
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#5 | |
|
Professional Programmer
|
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
|
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
#include <cstdlib>
In cstdlib, NULL is defined. EDIT: damn, jayme was 2 minutes faster ![]() |
|
|
|
|
|
#7 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|