Hey guys, I have been fighting with this for hours, but cannot seem to fix it.I am trying to make a copy constructor and an assignment operator use other classes one.
I have a circular dependency of a Stack, then implement a queue in terms of a stack, then a list in terms of a queue.
here is the error I am getting.
1 2
List.cpp:17:10: error: comparison between distinct pointer types ‘List<int>*’ and ‘Queue<int>* const*’ lacks a cast [-fpermissive]
if(this != &other.queue)
But I was taught that makes sure you dont copy itself
Yes. You must prevent the copying if this and &other are the same.
this is a pointer to the current object (this means it holds the current object's memory address).
You check it against the other object's memory address.
You do not check it against the memory address of other.queue, which is a member variable of other.
Also, be warned that in the C++ library there exists a container named std::queue.
You should probably not use the name "queue" in your code, especially if you are usingnamespace std;.
template <class T>
ostream& operator<<(ostream& os,List<T>& list)
{
Queue<T>* temp = new Queue<T>();
Queue<T>* t = new Queue<T>();
while(!list.queue->isEmpty())
{
temp->enqueue(list.queue->dequeue());
}
cout << "[";
while(!temp->isEmpty())
{
T num = temp->dequeue();
//cout << num <<endl;
list.queue->enqueue(num); //seems like this line does not work
os << num;
if(!temp->isEmpty())
{
//list.queue->enqueue(num);
cout << ",";
}
}
cout << "]";
}
here I am attempting to print the results, but not deleting the queue.But it seems like list.queue(num); does not work.I am confused now guys.What am I really doing wrong
You are overusing dynamic memory allocation (pointers + new).
Then you have memory leaks because you don't deallocate the memory by delete'ing the pointers.
1 2 3 4 5 6 7 8 9 10
// instead of...
Queue<T>* temp = new Queue<T>();
Queue<T>* t = new Queue<T>();
// ...
delete temp;
delete t;
// do this:
Queue<T> temp;
Queue<T> t;
Another thing: the output operation should not modify the object which is output.
In this case, the code should work even if we pass list by const reference.
(Also just as in the case of queue vs std::queue, there's list vs std::list, you've been warned. Twice.)
Thanks alot Catfish.
It seems to be working now.The reason I dont use the assignment operator is because for some reason the List one is not working well.
I am confused as to how can that occur.The stack and the queue one are working fine, but not the List one.
Both Queue<T>::operator= and List<T>::operator= leak memory. In the case of the List twice as much memory is leaked as it also uses the Queue version. If there is anything wrong with List<T>::operator=, it is also wrong with Queue<T>::operator=.
Does it not seem a little perverse that Stack uses a linked list for it's implementation, and you're ultimately using a Stack for a linked list implementation? It would make a lot more sense for the List to stand alone, and to implement Queue in terms of the List, and Stack in terms of the Queue. You usually want to start with the general and proceed to the specific (or more restricted) which is the opposite of what you're doing here.