Link List

I am working on a Link List. I'm a little confused with nullptr, and how to declare it. From this snippit can you help me figure out why it's creating an error?
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
struct Node
{
    int number;
    int counter;
    Node* link;
};

class OrderedList
{
    private:
    Node* head;
    public:
    OrderedList();
    int size();
    void insert(int val); //insert in sorted order
    double average();
    void remove(int val);
    void print();
    void remove7Multiples();
    ~OrderedList();
};

OrderedList::OrderedList()
{
    head = nullptr;  //Error occurs here
}
Last edited on
Show the exact error message.
head = nullptr; //Error occurs here
Setting a pointer to null does not have any side-effects, so the error can't be there.

The error is most likely at some point *after* the OrderedList is constructed.
Last edited on
|error: 'nullptr' was not declared in this scope| This is what it displays as the error. It repeats this error for each function where nullptr is being used, with the same error message.

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
#include <iostream>
#include <fstream>
using namespace std;

struct Node
{
    int number;
    int counter;
    Node* link;
};

class OrderedList
{
    private:
    Node* head;
    public:
    OrderedList();
    int size();
    void insert(int val); //insert in sorted order
    double average();
    void remove(int val);
    void print();
    void remove7Multiples();
    ~OrderedList();
};

OrderedList::OrderedList()
{
    Node* head = nullptr;
}

int OrderedList::size()
{
    int count =0;
    Node* curr = head;
    while(curr != nullptr)
    {
        count++;
        curr = curr->link;
    }
return count;
}

void OrderedList::insert(int val) //insert in sorted order
{
    Node* prev = nullptr;
    Node* curr = head;
    //find place to insert
    while(curr != nullptr)
    {
        if(val == curr->number)
        {
            curr->counter++;
            return;
        }
            else if(val < curr->number)
                break;
        else
        {
            prev = curr;
            curr = curr->link;
        }
    }
    Node* n = new Node();
    n->number = val;
    n->counter = 1;
    n->link = curr;
    if(prev == nullptr)
    head = n;
    else
    prev->link = n;
}

double OrderedList::average()
{
    double avg = 0;
    int n = size();
    Node* curr = head;
    while(curr != nullptr)
    {
        avg += curr->number;
        curr = curr->link;
    }
    if(n != 0)
        avg /= n;
return avg;
}

void OrderedList::remove(int val)
{
    Node* prev = nullptr;
    Node* curr = head;
    while(curr != nullptr)
    {
        if(val == curr->number)
        {
            break;
        }
            else if(val < curr->number) //can't find anymore since we already reached a higher number in list
                return;
        else
        {
            prev = curr;
            curr = curr->link;
        }
    }
    if(curr == nullptr)//not found
        return;
    if(prev == nullptr)
    head = curr->link;
    else
    prev->link = curr->link;
    delete curr;
}

void OrderedList::remove7Multiples()
{
    Node* prev = nullptr;
    Node* curr = head;
    while(curr != nullptr)
    {
        if(curr->number % 7 == 0 )
        { //found a multiple of 7
            if(prev == nullptr)
            head = curr->link;
            else
            prev->link = curr->link;
            Node* next = curr->link;
            delete curr;
            curr = next;
        }
        else
        {
            prev = curr;
            curr = curr->link;
        }
    }
}

void OrderedList::print()
{
    int n = size();
    Node* curr = head;
    for(int i = 1; i <= n; i++)
    {
        cout << curr->number << "-" << curr->counter << "\t";
        curr = curr->link;
        if(i % 8 == 0)
        {
            cout << endl;
        }
    }
    cout << endl;
}

OrderedList::~OrderedList()
{
    Node* temp;
    while(head != nullptr)
    {
        temp = head->link;
        delete temp;
        head = temp;
    }
}

int main()
{
    string filename = "Link.in";
    ifstream infile(filename.c_str());
    OrderedList olist;
    int num;
    if(infile.fail())
    {
        cout << "ERROR: could not open input file "<< filename << endl;
        return 1;
    }
        while(infile >> num)
        {
            olist.insert(num);
        }
    infile.close();
    olist.print();
    cout << "No. of nodes = " << olist.size() << endl;
    cout << "Average = " << olist.average() << endl << endl;
    cout << "Deleting multiples of 7" << endl;
    olist.remove7Multiples();
    olist.print();
    cout << "No. of nodes = " << olist.size() << endl;
    cout << "Average = " << olist.average() << endl << endl;
}
Last edited on
I assumed you meant a runtime error. This is why actually pasting the text of the error is important.

You have an outdated compiler that doesn't have C++11 on by default.
If GCC or clang, add flag -std=c++11.

If that doesn't work, #yolo just change each nullptr to NULL.

Also you are declaring a local variable called "head" in your latest code. Use the existing class variable.
Last edited on
nullptr was added to the language with C++11, so you need to compile it using /std:c++11 or later.

VS 2019 defaults to C++14 and has zero compile problems with your full code. VS 2017 required including <string>, I added it in VS 2019. (You create a string in main)

159
160
161
162
163
164
    while(head != nullptr)
    {
        temp = head->link;
        delete temp;
        head = temp;
    }

Why are you deleting temp and then assigning it to your head? You are assigning now unowned memory to your head.

VS 2019 warns
Warning C6001 Using uninitialized memory 'temp'. Line 163

Thank you for that, I had no idea I needed to alter settings for the compiler.
Topic archived. No new replies allowed.