I am trying to assign a variable to a return value but the deconstructor is called before the values are assigned to pq. I also get the following error which I'm assuming is an attempt to double delete a variable:
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
PriorityQueue pq;
pq = FindThroughPaths(a, b);
FindThroughPaths(a, b) returns a PriorityQueue.
I may be getting this error because the return value has dynamically allocated member variables.
I am simply trying to find a way to keep the return value of FindThroughPaths(a, b) within the scope and I do not want to create multiple instances just to access it's members. This is an example of what I would like:
PriorityQueue pq;
pq = FindThroughPaths(a, b);
while (!pq.IsEmpty) {
Path tmp = pq.Dequeue();
std::cout << "\n" << tmp.name << " #" << tmp.number;
}
The constructor, deconstructor, and assignment operator for PriorityQueue:
PriorityQueue::PriorityQueue()
{
std::cout << "\nConstructor called." << endl;
items.elements = new ItemType[maxItems];
length = 0;
}
PriorityQueue::~PriorityQueue()
{
std::cout << "\nDeconstructor called." << endl;
delete[] items.elements;
}
PriorityQueue PriorityQueue::operator=(const PriorityQueue &originalPQ)
{
//This function is started after the return value, the right operand, has been deconstructed.
std::cout << "\nAssignment called." << endl;
for (int i = 0; i < maxItems; i++)
items.elements[i] = originalPQ.items.elements[i];
return *this;
}