Error when compiling a program

Write your question here.
When I compile an example program by Karlina Beringer I get 2 errors.

C:\Martin\MalikChapter3\LinkedList.cpp In member function 'bool LinkedList::insertNode(node*, int)':
46 13 C:\Martin\MalikChapter3\LinkedList.cpp [Error] 'length' was not declared in this scope

C:\Martin\MalikChapter3\LinkedList.cpp In member function 'void LinkedList::printList()':
113 35 C:\Martin\MalikChapter3\LinkedList.cpp [Error] 'count' was not declared in this scope

31 C:\Martin\MalikChapter3\Makefile.win recipe for target 'LinkedList.o' failed
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  Put the code you need help with here.
//*******************************************************************
//  LinkedList.cpp
//  LinkedList_Project
//
//  Created by Karlina Beringer on June 12, 2014.
//  This source file contains the LinkedList class definitions.
//*******************************************************************

#include "LinkedList.h"
//int count = 0;
// Default Constructor creates the head node.
LinkedList::LinkedList()
{
    head -> song = "head (contains no song data)";
    head -> artist = "head (contains no artist data)";
    head -> next = NULL;
    listLength = 0;
}

// Setter adds a node to the list at a given position.
// Takes a node and list position as parameters.
// Position must be between 1 and the number of data nodes.
// Returns true if the operation is successful.
bool LinkedList::insertNode( node * newNode, int position )
{
    if ((position <= 0) || (position > listLength + 1))
    {
        cout << "nError: the given position is out of range.n";
        return false;
    }
    if (head -> next == NULL) 
    {
        head -> next = newNode;
        listLength++;
        return true;
    } 
    int count = 0;
    node * p = head;
    node * q = head;
    while (q)
    { 
        if (count == position)
        {
            p -> next = newNode;
            newNode -> next = q;
            length++;
            
			return true;
        }
        p = q;
        q = p -> next;
        count++;
    }
    if (count == position)
    {
        p -> next = newNode;
        newNode -> next = q;
        listLength++;
        return true;
    }
    cout << "nError: node was not added to list.n";
    return false;
}

// Setter removes a node by its given position.
// Returns true if the operation is successful.
bool LinkedList::removeNode( int position )
{
    if ((position <= 0) || (position > listLength + 1))
    {
        cout << "nError: the given position is out of range.n";
        return false;
    }
    if (head -> next == NULL)
    {
       cout << "nError: there is nothing to remove.n";
       return false;
    }
    int count = 0;
    node * p = head;
    node * q = head;
    while (q) 
    {
        if (count == position)
        {
            p -> next = q -> next;
            delete q;
            listLength--;
            return true;
        }
        p = q;
        q = p -> next;
        count++;
    }
    cout << "nError: nothing was removed from the list.n";
    return false;
}

// Prints each node in the list in consecutive order,
// starting at the head and ending at the tail.
// Prints the data to the console.
void LinkedList::printList()
{
	
    node * p = head;
    node * q = head;
    cout << "n---------------------------n";
    cout << "Song Playlist n";
    while (q)
    {
        p = q;
        cout << "n-----------------------------n";
        cout << "t position: " << count << endl;
        cout << "t song: " << p -> song << endl;
        cout << "t artist: " << p -> artist << endl;
        q = p -> next;
        count++;
    }
}

// Destructor de-allocates memory used by the list.
LinkedList::~LinkedList() 
{
    node * p = head;
    node * q = head;
    while (q)
    {
        p = q;
        q = p -> next;
        if (q) delete p;
    }
}
Well, you really should define these two variables.
Can't say for sure without seeing your class definition, but I'd suggest
line 47 should be listLength++; not length++; as it currently is.
Also unless count is a member variable or global data (global variable named "count" is a shooting offence by the way) that has been misspelled, it appears to be superfluous in line 118 and the line should be deleted.
If it is defined elsewhere, then you're either out of scope or mistyped it in line 118.
If I delete count++ in line 118 and also delete count in line 114, the program compiles OK but when I run it, it tries to run with that black console screen but then bombs out after a few seconds.
When you say "bombs out" do you get an actual error or does the program just run to it's normal conclusion? What's the final exit/return code/message?

Rather than deleting count in both places, just define it, after line 109, put int count = 0; and use count as normal in the former lines 114 and 118.

Just in case your program is exiting normally and closing down the console, add this as the last line in main, before the return, so the console stays up until you hit enter. This should show whether it's just running very quickly or actually crashing.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    ...
    ...
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return (0);
}


Alternatively, just run the program directly from the command line, and you'll be able to see everything.

Also, more code, for proper context.
There are some odd things in this code. There's a definition for a global variable count, that's been commented out - why?

The functions InsertNode() and RemoveNode() have local variables also called count, which would hide the global variable, if it weren't commented out. In each case, they initialise the variable to zero before using it, so it would seem they're not intending to use a global variable.

The function printList() attempts to use a variable called count - presumably, the global variable that's been commented out. But there's no need for it to use a global variable, because nothing else attempts to use it.

Is this your code? Have you copied it from somewhere? Have you modified it? Have you made an error and not copied it properly?
Here is the complete listing of the program. Maybe this will help.
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//*******************************************************************
//  LinkedList.h
//  LinkedList_Project
//
//  Created by Karlina Beringer on June 12, 2014.
//  This header file contains the LinkedList class declarations.
//*******************************************************************

#ifndef LinkedList_h
#define LinkedList_h

#include <iostream>
#include <string>
using namespace std;

//*******************************************************************
// Node structs contain data and a pointer to the next node.
// In this project, it will represent a song/artist combination.
//*******************************************************************
struct node
{
    string song;
    string artist;
    node * next;
};

//*******************************************************************
// LinkedList is a list of singly-linked nodes.
// In this project, it will represent a song playlist.
//*******************************************************************
class LinkedList
{
private:
    // Head of the list contains no song data, 
    // but points to the song playlist.
    node * head;
    int listLength;
    
public:
    // Default Constructor creates the head node.
    LinkedList();
    
    // Setter adds a node to the list at a given position.
    // Takes a node and list position as parameters.
    // Position must be between 1 and the number of data nodes.
    // Returns true if the operation is successful.
    bool insertNode( node * newNode, int position );
    
    // Setter removes a node by its given position.
    // Returns true if the operation is successful.
    bool removeNode( int position );

    // Prints each node in the list in consecutive order,
    // starting at the head and ending at the tail.
    // Prints list data to the console.
    void printList();
    
    // Destructor de-allocates memory used by the list.
    ~LinkedList();
};

#endif

//*******************************************************************
//  LinkedList.cpp
//  LinkedList_Project
//
//  Created by Karlina Beringer on June 12, 2014.
//  This source file contains the LinkedList class definitions.
//*******************************************************************

#include "LinkedList.h"

// Default Constructor creates the head node.
LinkedList::LinkedList()
{
    head -> song = "head (contains no song data)";
    head -> artist = "head (contains no artist data)";
    head -> next = NULL;
    listLength = 0;
}

// Setter adds a node to the list at a given position.
// Takes a node and list position as parameters.
// Position must be between 1 and the number of data nodes.
// Returns true if the operation is successful.
bool LinkedList::insertNode( node * newNode, int position )
{
    if ((position <= 0) || (position > listLength + 1))
    {
        cout << "nError: the given position is out of range.n";
        return false;
    }
    if (head -> next == NULL) 
    {
        head -> next = newNode;
        listLength++;
        return true;
    } 
    int count = 0;
    node * p = head;
    node * q = head;
    while (q)
    { 
        if (count == position)
        {
            p -> next = newNode;
            newNode -> next = q;
            listLength++;
            return true;
        }
        p = q;
        q = p -> next;
        count++;
    }
    if (count == position)
    {
        p -> next = newNode;
        newNode -> next = q;
        listLength++;
        return true;
    }
    cout << "nError: node was not added to list.n";
    return false;
}

// Setter removes a node by its given position.
// Returns true if the operation is successful.
bool LinkedList::removeNode( int position )
{
    if ((position <= 0) || (position > listLength + 1))
    {
        cout << "nError: the given position is out of range.n";
        return false;
    }
    if (head -> next == NULL)
    {
       cout << "nError: there is nothing to remove.n";
       return false;
    }
    int count = 0;
    node * p = head;
    node * q = head;
    while (q) 
    {
        if (count == position)
        {
            p -> next = q -> next;
            delete q;
            listLength--;
            return true;
        }
        p = q;
        q = p -> next;
        count++;
    }
    cout << "nError: nothing was removed from the list.n";
    return false;
}

// Prints each node in the list in consecutive order,
// starting at the head and ending at the tail.
// Prints the data to the console.
void LinkedList::printList()
{
    node * p = head;
    node * q = head;
    cout << "n---------------------------n";
    cout << "Song Playlist n";
    while (q)
    {
        p = q;
        cout << "n-----------------------------n";
        cout << "t position: " << count << endl;
        cout << "t song: " << p -> song << endl;
        cout << "t artist: " << p -> artist << endl;
        q = p -> next;
        count++;
    }
}

// Destructor de-allocates memory used by the list.
LinkedList::~LinkedList() 
{
    node * p = head;
    node * q = head;
    while (q)
    {
        p = q;
        q = p -> next;
        if (q) delete p;
    }
}

//*******************************************************************
//  main.cpp
//  LinkedList_Project
//
//  Created by Karlina Beringer on June 12, 2014.
//  This driver implements the LinkedList class.
//*******************************************************************

#include "LinkedList.h"
using namespace std;

int main()
{
    // STEP 1: Create some unlinked song nodes.
    node * A = new node;
    A -> song = "We Are";
    A -> artist = "Vertical Horizon";
    
    node * B = new node;
    B -> song = "I Stand Alone";
    B -> artist = "Godsmack";
    
    node * C = new node;
    C -> song = "Heir Apparent";
    C -> artist = "Opeth";
    
    node * D = new node;
    D -> song = "Fear of the Dark";
    D -> artist = "Iron Maiden";
    
    node * E = new node;
    E -> song = "Blue Monday";
    E -> artist = "New Order";
    
    node * F = new node;
    F -> song = "The Moth";
    F -> artist = "Aimee Mann";
    
    // STEP 2: Build a list of three song nodes by appending to end of list.
    LinkedList myList;
    myList.insertNode(A, 1);
    myList.insertNode(B, 2);
    myList.insertNode(C, 3);
    myList.insertNode(D, 4);
    myList.printList();
    
    // STEP 3: Insert a node into middle of list.
    myList.insertNode(E, 2);
    myList.printList();
    
    // STEP 4: Insert node at the front of list.
    myList.insertNode(F,1);
    myList.printList();
    
    // STEP 5: Remove the last node from the list.
    myList.removeNode(6);
    myList.printList();
    
    // STEP 6: Remove the first node from the list.
    myList.removeNode(1);
    myList.printList();
    
    // STEP 7: Remove a node from the middle of the list.
    myList.removeNode(3);
    myList.printList();
    
    return 0;
}
problem is here
LinkedList::LinkedList()
{
head -> song = "head (contains no song data)";
head -> artist = "head (contains no artist data)";
head -> next = NULL;
listLength = 0;
}

Assign the memory to head first

i mean

LinkedList::LinkedList()
{
head =new node;
head -> song = "head (contains no song data)";
head -> artist = "head (contains no artist data)";
head -> next = NULL;
listLength = 0;
}
and u can comment count as it is not used by while loop
Last edited on
You are a star sylphsang!!!! It worked.......
I have been stuck with program for the past two weeks.....

thank you to all who have tried to help.
Topic archived. No new replies allowed.