However, with this one I just can't see where I'm going wrong. The code compiles fine, and it all works, unless I try to delete the top member of the queue. Originally, it was calling an Assert error, but after I removed the assert header (as I was not using it all that much), it changed to a Breakpoint, but couldn't point to a specific point in the code.
// queue.h
// This file declares a data structure for a queue
#pragma once
template <class Tx, class Ty, int Nx> struct Node
{
Tx dataX[Nx];
Ty dataY;
Node *next;
};
template <class Tx, class Ty, int Nx> class Queue
{
Node<Tx, Ty, Nx> *first, *last;
public:
int size;
Queue();
~Queue();
void add(Tx _dataX[Nx], Ty _dataY);
void insert(Tx _dataX[Nx], Ty _dataY);
void getX(Tx (&_dataX)[Nx]);
void getY(Ty (&_dataY));
void del();
};
template <class Tx, class Ty, int Nx> Queue<Tx, Ty, Nx>::Queue()
{
first = NULL;
last = NULL;
size = 0;
}
template <class Tx, class Ty, int Nx> Queue<Tx, Ty, Nx>::~Queue()
{
while(first != NULL) del();
}
template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::add(Tx _dataX[Nx], Ty _dataY)
{
if(last == NULL)
{
first = new Node<Tx, Ty, Nx>;
for(int i=0;i<Nx;i++) first->dataX[i] = _dataX[i];
first->dataY = _dataY;
last = first;
}
else
{
last->next = new Node<Tx, Ty, Nx>;
last = last->next;
for(int i=0;i<Nx;i++) last->dataX[i] = _dataX[i];
last->dataY = _dataY;
}
size++;
}
template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::insert(Tx _dataX[Nx], Ty _dataY)
{
if(first == NULL)
{
first = new Node<Tx, Ty, Nx>;
for(int i=0;i<Nx;i++) first->dataX[i] = _dataX[i];
first->dataY = _dataY;
}
else
{
Node<Tx, Ty, Nx> *tmp = new Node<Tx, Ty, Nx>;
for(int i=0;i<Nx;i++) tmp->dataX[i] = _dataX[i];
tmp->dataY = _dataY;
tmp->next = first;
first = tmp;
}
size++;
}
template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::getX(Tx (&_dataX)[Nx])
{
for(int i=0;i<Nx;i++) _dataX[i] = first->dataX[i];
}
template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::getY(Ty (&_dataY))
{
_dataY = first->dataY;
}
template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::del()
{
if(first != NULL)
{
Node<Tx, Ty, Nx> *tmp = first;
first = first->next;
delete tmp;
size--;
}
}
I'm using VC++ EE 2008, on a Windows XP Operating System. Though, as far as I'm aware, this shouldn't matter with the code I'm writing.
It took me a while to nail down where the error was in the code, as (mentioned before) it did not give me a line to work with. Eventually I commented out the delete line in the del() function, and voila, it ran smoothly (save for the memory leak).
Can anyone help in some way? I'm still pretty new to C++, and I'm sure it could quite possibly be terribly coded.

New Topic/Question
Reply



MultiQuote




|