0

A homework assignment calls for the overloading of the consignment operator for a Heap class that is used to store pointers to queues at every node in an attempt to emulate a Emergency room priority queue.

So far here is the code I've come up with:

    template <typename T>
    Heap<T>& Heap<T>::operator=(const Heap<T> & rhs)
    {
        //check for self-assignment
        if(this != &rhs)
        {
            //delete memory
            for(int i = 0; i < MAX_HEAP; i++)
            {
                //delete queue at position i, set pointer to NULL
                delete items[i];
                items[i] = NULL;
            }//end for
            delete * items;

            //create new memory to hold copy of rhs

                        //error occurs here
            items = new queue<T> *[MAX_HEAP] ;

            for(int i = 0; i < MAX_HEAP; i++)
                items[i] = rhs.items[i];

            //assigns new stuff to this heap
            size = rhs.size;
            nodes = rhs.nodes;

        }//end if
        return *this;
    }//end =

items is declared in the header file as such:

    queue<T>* items[MAX_HEAP];

and is an instance of the standard library queue.

I am not quite sure if I am using the proper syntax for creating a new dynamic array of queue pointers.

I am getting an error that says:

    error C2440: '=' : cannot convert from 'std::queue<_Ty> **' to 'std::queue<_Ty> *[50]'

Any ideas as to what might cause it and how I can solve it?

Bart
  • 19,692
  • 7
  • 68
  • 77
D1990c
  • 11
  • 4
  • 2
    For significantly less pain, use the [copy-and-swap idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). – James McNellis Apr 26 '12 at 20:34
  • 1
    Why don't just use `std::vector` instead of C arrays and manual memory management? Mixing standard library classes and C arrays together is idiotic overkill. – Griwes Apr 26 '12 at 20:34
  • How is a heap going to use a dynamic array of queue pointers? – Bo Persson Apr 26 '12 at 20:43

2 Answers2

1

You've got items declared as std::queue<T> *items[50];. use std::queue<T> ** items;, or, better, std::vector<std::queue<T>> items;

Also, the delete * items; should be delete[] items;

EDIT - I forgot to add <T> after std::queue

nothrow
  • 15,882
  • 9
  • 57
  • 104
  • typo there, not provided class template type argument to std::queue :) – M3taSpl0it Apr 26 '12 at 20:37
  • assuming we used std::queue ** items, how would we set the size of the dynamic array? – D1990c Apr 26 '12 at 20:40
  • via the `new` operator. `new std::queue* [MAX_HEAP]` will return new dynamic (C-like!) array of std::queue pointers. Otherwise, you don't need `new`, if you write `std::queue* items[50];`, there is no need to `new[]` it, as it will be created with object containing the array. – nothrow Apr 26 '12 at 20:46
  • But you probably don't want that either. I have never seen a heap that is an array of pointers to queues. And why 50 of them? – Bo Persson Apr 26 '12 at 20:50
0

new returns a pointer to the passed in type, so it's mismatched now. I can't tell whether your declaration for items or the type passed to new is the one you mean, but the one passed to new has one more level of indirection than items.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192