Hi! I'm trying to add two "queues" together to make a queue that is a combination of the first and second queues. For instance, if queue1 = 1 2 3 and queue2 = 4 5 6, then the result returned would be 1 2 3 4 5 6.
For my operator+ overload, I keep getting the error my_queue my_queue::operator+(my_queue&, my_queue&)' must take either zero or one argument
I'm really not sure what's wrong. Here is my code:
For example... your queue has a 'myqueue' vector. So in your 'print' function, you are using 'this' without realizing it:
1 2 3 4 5
void my_queue::print()
{
// print the size of the queue
cout << myqueue.size();
}
In this example, we're printing the size of the myqueue vector. But which myqueue vector? Each my_queue object has its own myqueue vector! There could be hundreds!
The answer is.. we're printing "this" myqueue. IE: the one belonging to whichever object our print function was invoked on:
1 2 3 4 5 6 7
my_queue foo;
foo.print(); // <- inside 'print'... 'this' will point to foo.
// therefore the function will print foo's myqueue
// on a side note: it would be less confusing if myqueue and my_queue
// weren't so similarly named ;P
Blah blah blah... long story short... for 'this' you don't need to prefix anything:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// I've just copy/pasted your original + operator but removed q1:
my_queue my_queue::operator+(my_queue q2){
vector<int> queue_result;
if ( /*q1.*/size() != q2.size()) { // <- remove q1... just use this size
throw std::runtime_error("Can't add two queues of different sizes!");
}
for(int i = 0; i</*q1.*/size(); i++){ // <- here too
queue_result.enqueue(/*q1.*/at(i)); // <- and here
queue_result.enqueue(q2.at(i));
}
return queue_result;
}