Feb 18, 2020 at 10:54pm UTC
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 Feb 18, 2020 at 10:55pm UTC
Feb 18, 2020 at 10:57pm UTC
Show the exact error message.
Feb 18, 2020 at 11:02pm UTC
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 Feb 18, 2020 at 11:03pm UTC
Feb 19, 2020 at 12:15am UTC
|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 Feb 19, 2020 at 12:17am UTC
Feb 19, 2020 at 1:07am UTC
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 Feb 19, 2020 at 1:09am UTC
Feb 19, 2020 at 4:31am UTC
Thank you for that, I had no idea I needed to alter settings for the compiler.