Overwriting the same node when loading file

I'm working on a project for class, and I was having problems with memory errors (fixed it, deleted my post). I'm no longer getting exceptions thrown from that, but I am having issues with overwriting data that was previously read in from my constructor that loads from file. I'm not sure if it's happening at that point, or if it's occurring when I call enqueue.

I'm still learning how to use pointers, LLL, etc, so I don't have a firm grasp on some of these concepts.



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
  
#include "queue.h"

/*class Queue
{
public: 
	Queue(); // default constructor
	Queue(char *qFilename); // constructor loaded from file
	Queue(const Queue& aQueue); // copy constructor
	~Queue(); // destructor

	const Queue& operator= (const Queue& aQueue);
	
	bool isEmpty() const; 
	bool enqueue(Group *aGroup);
	bool dequeue(Group &aGroup);
	bool peekFront(Group *&aGroup) const;
	void display() const;
	void writeQueueToFile(char *qFilename);
private:
	struct Node
	{
		Group *data;
		Node *next = nullptr;
	};// end struct
	Node *front;
	Node *rear;
	int size;
	
	void destroy(Node *&currHead); // helper function to deallocate memory
	
};*/

// default constructor
Queue::Queue() :front(nullptr), rear(nullptr)
{

}
// constructor loaded from file
Queue::Queue(char *qFilename) :front(nullptr), rear(nullptr)
{
	
	Group *aGroup = new Group(); 
	ifstream inFile;

	char tempGroupName[25];
	int  tempSpecialNeeds;
	int  tempPromos;
	int  tempGroupSize;

	inFile.open(qFilename);

	//verify file opened
	if (!inFile)
	{
		cerr << qFilename << " failed to open for input! " << endl;
		exit(1);
	}

	// import data
	inFile.get(tempGroupName, 25, ';');
	while (!inFile.eof())
	{
		
		// continue to read in file records 
		inFile.ignore(100, ';');
		inFile >> tempGroupSize;
		inFile.ignore(100, ';');
		inFile >> tempSpecialNeeds;
		inFile.ignore(100, ';');
		inFile >> tempPromos;	
		inFile.ignore(100, '\n');
	
		// populate data
		aGroup->setGroupName(tempGroupName);
		aGroup->setGroupSize(tempGroupSize);
		aGroup->setSpecialNeeds(tempSpecialNeeds);
		aGroup->setPromo(tempPromos);

		// enter current data into the queue
		enqueue(aGroup);

		Group *aGroup = new Group();
		// start reading in the next record
		inFile.get(tempGroupName, 25, ';');
	}
	inFile.close();
}

Queue::Queue(const Queue& aQueue) : front(nullptr), rear(nullptr) // copy constructor
{
	
	if (aQueue.front == nullptr)
	{
		front = rear = nullptr;
	}
	else
	{
		// copy first node 
		front = new Node;
		assert(front != nullptr); // check allocation
		front->data = aQueue.front->data;

		// copy the rest of the queue
		Node *destNode = front; // points to the last node in new queue
		Node *srcNode = aQueue.front->next; // points to node in aQueue

		while (srcNode)
		{
			destNode->next = new Node;
			assert(destNode->next != nullptr); // check allocation
			destNode = destNode->next;
			destNode->data = srcNode->data;

			srcNode = srcNode->next;
		}
		destNode->next = nullptr;

		// set rear pointer
		rear = destNode;
	}
}
const Queue& Queue::operator= (const Queue& aQueue)
{
	if (this == &aQueue)
		return *this;
	else
	{
		// release dynamically allocated memory held by current object
		Node * curr = front;
		while (front)
		{
			curr = front->next;
			delete front;
			front = curr;
		}

		// make a deep copy of aQueue
		if (aQueue.front == nullptr)
		{
			front = rear = nullptr;
		}
		else
		{
			// copy first node
			front = new Node;
			assert(front != nullptr); // check allocation
			front->data = aQueue.front->data;

			// copy the rest of the queue
			Node *destNode = front; // points to last node in new queue
			Node *srcNode = aQueue.front->next; // points to node in aQueue
			while (srcNode)
			{
				destNode->next = new Node;
				assert(destNode->next != nullptr); // check allocation
				destNode = destNode->next;
				destNode->data = srcNode->data;

				srcNode = srcNode->next;
			}
			destNode->next = nullptr;

			// set rear pointer 
			rear = destNode;
		}
		return *this;
	}

}
// destructor
Queue::~Queue()
{
	destroy(front);
	front = nullptr;
	rear = nullptr;
}

// helper function to deallocate memory
void Queue::destroy(Node *&currHead) 
{
	if (currHead)
	{
		destroy(currHead->next);
		delete currHead->data;
		delete currHead;
	}
}

// checks to see if list is empty
bool Queue::isEmpty() const
{
	return front == nullptr;
}
// adds a new group to the queue
bool Queue::enqueue(Group *aGroup)
{


	// add to the rear
	Node *newNode = new Node();
	assert(newNode);
	newNode->data = aGroup;
	newNode->next = nullptr;

	if (rear == nullptr)
	{
		front = rear = newNode;
	}
	else
	{
		rear->next = newNode;
		rear = newNode; // update rear
	}
	size++;
	
	return true;
}


// prints the queue to the screen
void Queue::display() const
{
	int position = 1;
	
	if (isEmpty())
		cout << "The queue is empty." << endl;

	cout << left << setw(20) << "Group Name " << setw(3) << "Position" << endl;
	for (Node *curr = front; curr; curr = curr->next)
	{
		cout << left << setw(20) << *(curr->data) << setw(3) << position << endl;
		position++;
	}
}


From Group class:
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
#include "group.h"
/*
class Group
{
public: 
	Group(); // default constructor
	Group(const char *groupName, int groupSize, int specialNeeds, int promo); // constructor with parameters
	Group(const Group& aGroup); // copy constructor
	~Group(); // destructor

	// accessor functions
	const char *getGroupName() const; 
	void getGroupName(char *groupName) const;
	int  getGroupSize() const;
	int  getSpecialNeeds() const;
	int  getPromo() const;
	
	// mutator functions
	void setGroupName(const char *groupName);
	void setGroupSize(int groupSize);
	void setSpecialNeeds(int specialNeeds);
	void setPromo(int promo);
	
	// print 
	void print(ostream& out)const;

	// operator functions
	const Group& operator=(const Group &g); 
	friend ostream& operator<<(ostream& out, const Group& g);

private:
	char *groupName;
	int	  groupSize;
	int	  specialNeeds; 
	int	  promo; 
		
	void init(const char *groupName, int groupSize, int specialNeeds, int promo); // initializer
};
bool operator==(const Group &g1, const Group &g2); 

*/
Group::Group() // default constructor
{
	groupName = nullptr;
	groupSize = 0;
	specialNeeds = 0;
	promo = 0;
}

// constructor with parameters
Group::Group(const char *groupName, int groupSize, int specialNeeds, int promo) 
{
	
	init(groupName, groupSize, specialNeeds, promo);
}

// copy constructor
Group::Group(const Group& aGroup)
{
	init(aGroup.groupName, aGroup.groupSize, aGroup.specialNeeds, aGroup.promo);
}
void Group::init(const char *groupName, int groupSize, int specialNeeds, int promo)
{
	this->groupName = new char[strlen(groupName) + 1];
	strcpy(this->groupName, groupName);
	this->groupSize = groupSize;
	this->specialNeeds = specialNeeds;
	this->promo = promo;
}
Group::~Group()
{
	if (this->groupName)
	{
		delete[] this->groupName;
	}
}
// accessor functions
const char *Group::getGroupName() const 
{
	return this->groupName;
}
void Group::getGroupName(char *groupName) const
{
	strcpy(groupName, this->groupName);
}

// mutator functions
void Group::setGroupName(const char *groupName)
{
	if (this->groupName)
	{ 
		delete[] this->groupName;
	}
	this->groupName = new char[strlen(groupName) + 1];
	strcpy(this->groupName, groupName);
}

// overload '=' operator
const Group& Group::operator=(const Group& g)
{
	if (this == &g)
		return *this;
	this->setGroupName(g.groupName);
	this->setGroupSize(g.groupSize);
	this->setSpecialNeeds(g.specialNeeds);
	this->setPromo(g.promo);
	return *this;
}
bool operator==(const Group &g1, const Group &g2)
{
	return strcmp(g1.getGroupName(), g2.getGroupName()) == 0;
}
// overload << operator
ostream& operator<<(ostream &out, const Group &g)
{
	g.print(out); 
	return out;
}
// print using  overloaded << operator
void Group::print(ostream& out)const
{
	out << this->groupName << ';' << this->groupSize << ';' << this->specialNeeds << ';'
		<< this->promo << endl;
}
Last edited on
> fixed it, deleted my post
don't do that, it's rude
http://www.cplusplus.com/forum/articles/40179/
also, some may have found faults in your solution


> overwriting data that was previously read in from my constructor
¿what data?
¿the first entry? ¿the last one?
¿how are you sure that it's being overwrtted? perhaps it was never enqueued

can't test your code as you didn't provide a main function and have no idea what kind of input do you expect.
I deleted it, because there were no responses. So, I figured I would just remove it. Sorry about that, I won't do that again. I will post the remainder of the relevant code. This is my first time posting, and I didn't know if I should post the whole program.

I'm actually not postive that it was being overwritten, now that you mention it. As it reads in, I can see all of the data records being read in (through cout statements not currently in the code), but when "display" runs afterward, it just prints the final record three times. That's why I assumed it was being overwritten.

Test data being read in from file "qfile.txt"
1
2
3
4
GroupName1;000;111;222
GroupName2;333;444;555
GroupName3;666;777;888
	


Here is the main function I am using to test 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

#include "group.h"
#include "queue.h"
#include <iostream>

using namespace std;

int main()
{

	//testing constructor
	
	Group *temp = new Group("Mutants", 31, 6, 1);
		
	char qFilename[] = "qFile.txt";
	Queue *waitlist = new Queue(qFilename);
	cout << " data set " << endl;
	waitlist->display();

	//test copy constructor
	Queue waitlistCopy(*waitlist);

	system("pause");
	return 0;
}


queue.h

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
#ifndef	QUEUE_H
#define QUEUE_H

#include <fstream>
#include <iomanip>
#include "group.h"

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

class Queue
{
public: 
	Queue(); // default constructor
	Queue(char *qFilename); // constructor loaded from file
	Queue(const Queue& aQueue); // copy constructor
	~Queue(); // destructor

	const Queue& operator= (const Queue& aQueue);

	bool isEmpty() const; 
	bool enqueue(Group *aGroup);
	bool dequeue(Group &aGroup);
	bool peekFront(Group *&aGroup) const;
	void display() const;
	void writeQueueToFile(char *qFilename);

private:
	struct Node
	{
		Group *data; 
		Node *next = nullptr;
	}; // end struct
	Node *front;
	Node *rear;
	int size;
	
	void destroy(Node *&currHead); // helper function to deallocate memory
	
};
#endif  


queue.cpp
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
#include "queue.h"

// default constructor
Queue::Queue() :front(nullptr), rear(nullptr)
{
	
}
// constructor loaded from file
Queue::Queue(char *qFilename) :front(nullptr), rear(nullptr)
{
	Group *aGroup = new Group(); 
	ifstream inFile;

	char tempGroupName[25];
	int  tempSpecialNeeds;
	int  tempPromos;
	int  tempGroupSize;

	inFile.open(qFilename);

	//verify file opened
	if (!inFile)
	{
		cerr << qFilename << " failed to open for input! " << endl;
		exit(1);
	}
	
	// import data
	inFile.get(tempGroupName, 25, ';');
	while (!inFile.eof())
	{	
		// continue to read in file records 
		inFile.ignore(100, ';');
		inFile >> tempGroupSize;
		inFile.ignore(100, ';');
		inFile >> tempSpecialNeeds;
		inFile.ignore(100, ';');
		inFile >> tempPromos;	
		inFile.ignore(100, '\n');
	
		// populate data
		aGroup->setGroupName(tempGroupName);
		aGroup->setGroupSize(tempGroupSize);
		aGroup->setSpecialNeeds(tempSpecialNeeds);
		aGroup->setPromo(tempPromos);
		
		// enter current data into the queue
		enqueue(aGroup);
	
		// start reading in the next record
		inFile.get(tempGroupName, 25, ';');
	}
	inFile.close();
}

Queue::Queue(const Queue& aQueue) : front(nullptr), rear(nullptr) // copy constructor
{
	
	if (aQueue.front == nullptr)
	{
		front = rear = nullptr;
	}
	else
	{
		// copy first node 
		front = new Node;
		assert(front != nullptr); 
		front->data = aQueue.front->data;

		// copy the rest of the queue
		Node *destNode = front; 
		Node *srcNode = aQueue.front->next; 

		while (srcNode)
		{
			destNode->next = new Node;
			assert(destNode->next != nullptr); 
			destNode = destNode->next;
			destNode->data = srcNode->data;

			srcNode = srcNode->next;
		}
		destNode->next = nullptr;

		// set rear pointer
		rear = destNode;
	}
}
const Queue& Queue::operator= (const Queue& aQueue)
{
	if (this == &aQueue)
		return *this;
	else
	{
		// release dynamically allocated memory held by current object
		Node * curr = front;
		while (front)
		{
			curr = front->next;
			delete front;
			front = curr;
		}

		// make a deep copy of aQueue
		if (aQueue.front == nullptr)
		{
			front = rear = nullptr;
		}
		else
		{
			// copy first node
			front = new Node;
			assert(front != nullptr); // check allocation
			front->data = aQueue.front->data;

			// copy the rest of the queue
			Node *destNode = front; // points to last node in new queue
			Node *srcNode = aQueue.front->next; // points to node in aQueue
			while (srcNode)
			{
				destNode->next = new Node;
				assert(destNode->next != nullptr); // check allocation
				destNode = destNode->next;
				destNode->data = srcNode->data;

				srcNode = srcNode->next;
			}
			destNode->next = nullptr;

			// set rear pointer 
			rear = destNode;
		}
		return *this;
	}

}
// destructor
Queue::~Queue()
{
	destroy(front);
	front = nullptr;
	rear = nullptr;
}


void Queue::destroy(Node *&currHead) 
{
	if (currHead)
	{
		destroy(currHead->next);
		delete currHead->data;
		delete currHead;
	}
}

bool Queue::isEmpty() const
{
	return front == nullptr;
}

bool Queue::enqueue(Group *aGroup) 
{
	// add to the rear
	Node *newNode = new Node();
	assert(newNode);
	newNode->data = aGroup;
	newNode->next = nullptr;

	if (rear == nullptr)
	{
		front = rear = newNode;
	}
	else
	{
		rear->next = newNode;
		rear = newNode; // update rear
	}
	size++;
	return true;
}

bool Queue::dequeue(Group &aGroup)
{

	// empty queue
	if (isEmpty())
		return false;
	else
	{
		Node *temp = front;
		if (front == rear) // front is only node
			front = rear = nullptr;
		else
			front = front->next; // remove first in line
		temp->next = nullptr;
		aGroup = *(temp->data); 
		delete temp;
		return true;
	}
}
// shows the first entry in the queue 
bool Queue::peekFront(Group *&aGroup)const 
{

	if (isEmpty())
		return false;
	else
		aGroup = front->data;
	return true;
}
// prints the queue to the screen
void Queue::display() const
{
	int position = 1;
	
	if (isEmpty())
		cout << "The queue is empty." << endl;

	cout << left << setw(20) << "Group Name " << setw(3) << "Position" << endl;
	for (Node *curr = front; curr; curr = curr->next)
	{
		cout << left << setw(20) << *(curr->data) << setw(3) << position << endl;
		position++;
	}
}

void Queue::writeQueueToFile(char *qFilename)
{
	ofstream outFile;
	outFile.open(qFilename);
	if (!outFile)
	{
		cerr << qFilename << " failed to open for output! " << endl;
		exit(1);
	}

	Node *curr;			
	char groupName[25]; 
	int groupSize = 0, specialNeeds = 0, promos = 0;

	for (curr = front; curr; curr = curr->next)
	{
		curr->data->getGroupName(groupName); 
		groupSize = curr->data->getGroupSize(); 
		specialNeeds = curr->data->getSpecialNeeds();
		promos = curr->data->getPromo();
													
		outFile << groupName << ';' << groupSize << ';' << specialNeeds << ';' << promos;
	}
	outFile.close();
}
group.h
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
#ifndef GROUP_H
#define GROUP_H

#include <iostream> 
using namespace std;

class Group
{
public: 
	Group(); // default constructor
	Group(const char *groupName, int groupSize, int specialNeeds, int promo); // constructor with parameters
	Group(const Group& aGroup); // copy constructor
	~Group(); // destructor

	// accessor functions
	const char *getGroupName() const; 
	void getGroupName(char *groupName) const;
	int  getGroupSize() const;
	int  getSpecialNeeds() const;
	int  getPromo() const;
	
	//// mutator functions
	void setGroupName(const char *groupName);
	void setGroupSize(int groupSize);
	void setSpecialNeeds(int specialNeeds);
	void setPromo(int promo);
	
	// print 
	void print(ostream& out)const;

	// operator functions
	const Group& operator=(const Group &g); // overload '=' operator
	friend ostream& operator<<(ostream& out, const Group& g);

private:
	char *groupName;
	int	  groupSize;
	int	  specialNeeds; // write function with if statement
	int	  promo; // write function to prompt for contact info
		
	void init(const char *groupName, int groupSize, int specialNeeds, int promo); // initializer
};
bool operator==(const Group &g1, const Group &g2); 
#endif 


group.cpp
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
#include "group.h"

Group::Group() // default constructor
{
	groupName = nullptr;
	groupSize = 0;
	specialNeeds = 0;
	promo = 0;
}

// constructor with parameters
Group::Group(const char *groupName, int groupSize, int specialNeeds, int promo) 
{
	init(groupName, groupSize, specialNeeds, promo);
}

// copy constructor
Group::Group(const Group& aGroup)
{
	init(aGroup.groupName, aGroup.groupSize, aGroup.specialNeeds, aGroup.promo);
}
void Group::init(const char *groupName, int groupSize, int specialNeeds, int promo)
{
	this->groupName = new char[strlen(groupName) + 1];
	strcpy(this->groupName, groupName);
	this->groupSize = groupSize;
	this->specialNeeds = specialNeeds;
	this->promo = promo;
}
Group::~Group()
{
	if (this->groupName)
	{
		delete[] this->groupName;
	}
}


const char *Group::getGroupName() const 
{
	return this->groupName;
}
void Group::getGroupName(char *groupName) const
{
	strcpy(groupName, this->groupName);
}
int  Group::getGroupSize() const
{
	return this->groupSize;
}

int  Group::getSpecialNeeds() const
{
	return this->specialNeeds;
}

int  Group::getPromo() const
{
	return this->promo;
}


void Group::setGroupName(const char *groupName)
{
	if (this->groupName)
	{ 
		cout << "if(this->groupName) " << endl;
		delete[] this->groupName;
	}
	this->groupName = new char[strlen(groupName) + 1];
	strcpy(this->groupName, groupName);
}

void Group::setGroupSize(int groupSize)
{
	this->groupSize = groupSize;
}

void Group::setSpecialNeeds(int specialNeeds)
{
	this->specialNeeds = specialNeeds;
}
void Group::setPromo(int promo)
{
	this->promo = promo;
}

const Group& Group::operator=(const Group& g)
{
	if (this == &g)
		return *this;
	this->setGroupName(g.groupName);
	this->setGroupSize(g.groupSize);
	this->setSpecialNeeds(g.specialNeeds);
	this->setPromo(g.promo);
	return *this;
}
bool operator==(const Group &g1, const Group &g2)
{
	return strcmp(g1.getGroupName(), g2.getGroupName()) == 0;
}

ostream& operator<<(ostream &out, const Group &g)
{
	g.print(out); 
	return out;
}
// print using  overloaded << operator
void Group::print(ostream& out)const
{
	out << this->groupName << ';' << this->groupSize << ';' << this->specialNeeds << ';'
		<< this->promo << endl;
}
set a breakpoint at the end of the function `Queue::Queue(char *qFilename)' and look at the list
you may see something like this
*this->front:             {data = 0x55555556cf10, next = 0x55555556f580}
*this->front->next:       {data = 0x55555556cf10, next = 0x55555556f5a0}
*this->front->next->next: {data = 0x55555556cf10, next = 0x0}
notice that all `data' have the same value, they all point to the same memory address, to the same object
so in the `display()' function you end up printing the same object three times

as a fix you could make enqueue() to copy its parameter
1
2
3
4
5
6
7
bool Queue::enqueue(Group *aGroup) 
{
	// add to the rear
	Node *newNode = new Node();
	assert(newNode);
	newNode->data = new Group(*aGroup); //<-- ~Node() should delete it
	newNode->next = nullptr;

however, you are abusing dynamic memory allocation

consider instead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Node{
	Group data; //not a pointer
	Node *next = nullptr;
};

Queue::Queue(char *qFilename) :front(nullptr), rear(nullptr)
{
	Group aGroup;  //not a pointer
	while(input(inFile, aGroup)) //you may benefit in creating this function
		enqueue(aGrroup);
}

bool Queue::enqueue(const Group &aGroup) //again, not a pointer
{
	// add to the rear
	// Node *newnode = new Node(aGroup); //may create this to use the copy constructor of Group
	Node *newNode = new Node();
	assert(newNode);
	newNode->data = aGroup; //copy the group, note that it uses operator= not the copy constructor
	newNode->next = nullptr;
	//...
}


Also Queue *waitlist = new Queue(qFilename); may be Queue waitlist(qFilename); instead.
Finally had a chance to get back to this project. Thank you for the detailed explanation! It's been really helpful.

This line in partcular, seems to have remedied the specific problem I had.
newNode->data = new Group(*aGroup);

Can you explain what you mean by " however, you are abusing dynamic memory allocation"? I'm not sure what that means.
you are using dynamic allocation when there is no need for it
for example, give me one good reason for using new here
1
2
3
4
5
6
int main()
{	
	Group *temp = new Group("Mutants", 31, 6, 1);
		
	char qFilename[] = "qFile.txt";
	Queue *waitlist = new Queue(qFilename);

also see http://www.cplusplus.com/forum/general/138037/#msg731921
Ah, I see, thank you for explaining. I was actually following an example to test my functions that was given by my instructor (who also suggested designing my node struct the way it was previously designed). Needless to say, with my lack of concrete knowledge on memory allocation and proper use of pointers, it quickly became a logistical nightmare for me to keep track of.

Your suggestion on how to write the node struct and enqueue functions is actually how I had it in the first place before I reached out to my instructor for guidance. I've reverted it back to the method you suggested, and now I'm just rewriting every line in my Queue class that references the old definition.

1
2
3
4
struct Node{
	Group data; //not a pointer
	Node *next = nullptr;
};


All of my memory-related issues seem to be gone. Now I can move on to finishing up the other functions. Thanks for your help!
Topic archived. No new replies allowed.