Operator overloading with vectors?

Hi!
I'm trying to overload the + operator to add vectors, and checking to make sure they added correctly in my main function. When I compile the two .cpp files, I get a VERY long error message. I was really sure this code is correct. What is it that I'm not understanding?

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
25
26
27
28
#include "chai.h"
#include <vector>
#include <iostream>
#include <stdexcept>

using std::vector;

int main(int argc, char** argv) {

	vector<int> a(3, 3);
	vector<int> b(3, 5);
	vector<int> c = a + b;
	vector<int> check_add;
	check_add.push_back(8);
	check_add.push_back(8);
	check_add.push_back(8);
	
	if (c == check_add) {
		std::cout<<"operator works for vector!\n";
	}
	else {
		std::cout<<"operator fails for vector!\n";
	}


	return 0;

}




chai.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef CHAI_H
#define CHAI_H

#include <vector>
#include <iostream>

using namespace std;

class chai{

	public:
		vector<int> operator+(const vector<int>& lhs, const vector<int>& rhs);

};

#endif 




chai.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 "chai.h"
#include <vector>
#include <iostream>
#include <stdexcept>

using std::vector;


vector<int> chai::operator+(const vector<int>& lhs, const vector<int>& rhs){	// return type is a vector of integers


	if(lhs.size() != rhs.size()){	// Vectors must be the same size in order to add them!
		throw std::runtime_error("Can't add two vectors of different sizes!");
	}

	vector<int> result;	// Declaring the resulting vector, result

	for(int i=0; i < lhs.size(); i++){	// adding each element of the result vector
		result.push_back(lhs.at(i) + rhs.at(i));	// by adding each element of the two together
	}

	return result;	// returning the vector "result"

}
Why do you have the operator+ inside the chai class?
Well, my professor supplied us with a main and wanted us to overload the + operator in a separate class.
Is operator+ not allowed to be inside of the chai class or something of the sorts? Because I've done something similar where I overloaded + to add complex numbers and I assumed this code would be quite similar.
Last edited on
If you make it a member of chai you have to declare operator+ with only one parameter because the first parameter is implicitly the chai object itself. If possible the right place to put it would be inside std::vector but you can't change the definition of std::vector so you don't really have any choice but to declare it outside classes.
Last edited on
Ah, I see.
I changed my code so that it's outside of classes, entirely. However, I'm still getting a long chain of errors.

It's updated to this:

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
25
26
27
28
#include "chai.h"
#include <vector>
#include <iostream>
#include <stdexcept>

using std::vector;

int main(int argc, char** argv) {

	vector<int> a(3, 3);
	vector<int> b(3, 5);
	vector<int> c = a + b;
	vector<int> check_add;
	check_add.push_back(8);
	check_add.push_back(8);
	check_add.push_back(8);
	
	if (c == check_add) {
		std::cout<<"operator works for vector!\n";
	}
	else {
		std::cout<<"operator fails for vector!\n";
	}


	return 0;

}




chai.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef CHAI_H
#define CHAI_H

#include <vector>
#include <iostream>

using namespace std;

vector<int> operator+(const vector<int>& lhs, const vector<int>& rhs);



#endif 




chai.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 "chai.h"
#include <vector>
#include <iostream>
#include <stdexcept>

using std::vector;


vector<int> operator+(const vector<int>& lhs, const vector<int>& rhs){	// return type is a vector of integers


	if(lhs.size() != rhs.size()){	// Vectors must be the same size in order to add them!
		throw std::runtime_error("Can't add two vectors of different sizes!");
	}

	vector<int> result;	// Declaring the resulting vector, result

	for(int i=0; i < lhs.size(); i++){	// adding each element of the result vector
		result.push_back(lhs.at(i) + rhs.at(i));	// by adding each element of the two together
	}

	return result;	// returning the vector "result"

}
What errors do you get?
When I just ran my code in Ubuntu instead of in Windows with gcc, it worked perfectly. So I think my problem was with gcc in Windows, instead of with my actual code. Whoops!

Thanks so much for the help before with realizing I don't need classes in separate files!
Last edited on
Topic archived. No new replies allowed.