Operator Overloading, Using this

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:

my_queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _MY_QUEUE_H
#define _MY_QUEUE_H

#include <vector>

class my_queue{

	public:
		my_queue();
		void enqueue(int In);
		int dequeue();
		void print();
		my_queue operator+(my_queue, my_queue);

	private:
		std::vector<int> myqueue;

};

#endif 



my_queue.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "my_queue.h"
#include <iostream>
#include <vector>
#include <stdexcept>

using namespace std;

my_queue::my_queue(){}

int my_queue::dequeue(){
	myqueue.erase(myqueue.begin());
}

void my_queue::enqueue(int In){
	myqueue.push_back(In);
}

void my_queue::print(){
	for(int i = 0; i < myqueue.size(); i++){
		cout << myqueue[i] << " ";
	}
	cout << "\n";
}

my_queue my_queue::operator+(my_queue q1, my_queue q2){

	vector<int> queue_result;

	if (q1.size() != q2.size()) {
		throw std::runtime_error("Can't add two queues of different sizes!");
	}

	for(int i = 0; i<q1.size(); i++){
		queue_result.enqueue(q1.at(i));
		queue_result.enqueue(q2.at(i));
		
	}

	return queue_result;
}



main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "my_queue.h"
#include <iostream>
#include <vector>
#include <stdexcept>

using namespace std;


int main(){
	my_queue queueObject1;
	my_queue queueObject2;

	queueObject1.enqueue(1);
	queueObject1.enqueue(2);
	queueObject1.enqueue(3);
	queueObject2.enqueue(4);
	queueObject2.enqueue(5);
	queueObject2.enqueue(6);

	queueObject1.print();
	queueObject2.print();

	vector<int> result = queueObject1 + queueObject2;
}
Last edited on
When overloading a binary operator like '+':

- If you make it a global function, you need 2 parameters: One for the left side of the +, and one for the right side of the +:

1
2
3
4
5
// note:  global
my_queue operator + (const my_queue& lhs, const my_queue& rhs)
{
    // combine 'lhs' and 'rhs'
}



- If you make it a member function, you need only 1 parameter. The left side is this and the right side is the parameter:

1
2
3
4
5
// note:  member function
my_queue my_queue::operator + (const my_queue& rhs)
{
    // combine '*this' and 'rhs'
}



You are have a member function.... but are passing 2 params. Either get rid of one of the params, or make it global.
Oh, I see. Thank you! :D
I have no idea how to use *this. Could someone explain it further?

And how would you compare the sizes (line 29 of my_queue.cpp) using *this?
this is the object on which you're acting.

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;
}
Ah, I see. Thanks for that explanation :D


To use this, you can just use size() alone? Because when I just got rid of q1, it would tell me that size() wasn't declared. Same goes for at.
To use this, you can just use size() alone? Because when I just got rid of q1, it would tell me that size() wasn't declared. Same goes for at.


Does your 'my_queue' class have size() or at() members?
It does not.


What did you mean by
if ( /*q1.*/size() != q2.size()) { // <- remove q1... just use this size
Last edited on
It does not.


Well that would be why.

'this' and 'q2' are of type my_queue. If my_queue does not have a size() member... then doing q2.size() makes no sense.

You probably meant to get the size of the 'myqueue' vector. In which case you'd have to do this:

 
if( myqueue.size() != q2.myqueue.size() )
OH! Alrighty, I was trying to do the latter. That makes a lot more sense. My code works now. Thanks! :D
Topic archived. No new replies allowed.