I am getting an error in the function empty()

I am not sure as to why I get an error in line 36 because it is defined
like that in the book?

1
2
3
4
5
6
7
8
9
10
11
  Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Dev-Cpp\newHeadInsert.cpp" -o "C:\Dev-Cpp\newHeadInsert.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" 
C:\Dev-Cpp\newHeadInsert.cpp: In member function `bool linkedList::empty() const':
C:\Dev-Cpp\newHeadInsert.cpp:36: error: expected primary-expression before '==' token
C:\Dev-Cpp\newHeadInsert.cpp: In function `int main()':
C:\Dev-Cpp\newHeadInsert.cpp:48: error: `empty' undeclared (first use this function)
C:\Dev-Cpp\newHeadInsert.cpp:48: error: (Each undeclared identifier is reported only once for each function it appears in.)

Execution terminated
 

here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<list>
#include<cassert>
using namespace std;

struct Node
{
int data;
Node *link;
};
typedef Node *NodePtr, *head, *first;
class linkedList
{
public:
       void head_insert(NodePtr& head, int the_number);
       //Precondition: The pointer variable head points to
       //the head of a linked list.
       //Postcondition: A new node containing the_number
       //has been added at the head of the linked list.
       bool empty() const;
};
//Function Definition
void linkedList::head_insert(NodePtr& head, int the_number)
 //void head_insert(NodePtr& head, int the_number)
 {
 NodePtr temp_ptr;
 temp_ptr = new Node;

 temp_ptr->data = the_number;

 temp_ptr->link = head;
 head = temp_ptr;
 }
 bool linkedList::empty() const
 {
            return (first == NULL);
            } 
 int main()
{
    linkedList list;
    NodePtr head, tmp, first;
    int num;
//    head = new Node(0, NULL);
    list.head_insert(*&head, num);
    //head = new Node(1, NULL);
    //head_insert(head, num);
//Display the node
          while(!empty())
          {
tmp = head;
cout << tmp->data << " " <<endl;
     tmp->link;
}
}
     
There is no free function name empty(). I would guess that you want the member function of linkedList: while(!list.empty()).

Actually then you have an infinite loop. You would guess you want something like this:
1
2
3
4
5
6
          tmp = head;
          while(tmp)
          {
cout << tmp->data << " " <<endl;
     tmp = tmp->link;
}
Your linkedList class doesn't (at the moment) have a member called first. I think there is also likely to be future confusion over "head" and "first".

Any chance of sorting out the indentation?
Last edited on
Topic archived. No new replies allowed.