Operator Overloading

Hi! I'm having trouble figuring out why my program crashes after the test for if the right stream works for a single value.

The output/error:

Right stream works for single value! +10
terminate called after throwing an instance of 'std::runtime_error'
what(): The vector can't be empty!

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <vector>
#include <stdexcept>

using namespace std;


vector<int>& operator<<(vector<int>& v, int x){	// Using vector<int>& because we are going to modify the vector in the parameter!
	v.push_back(x);	// Modifying vector v by adding integers to the back of it
	return v;	// Returning the modified vector!
}


vector<int>& operator>>(vector<int>& v, int& x){	// Using vector<int>& in declaration because we are modifying the returned vector from the parameter!
										// Using int& x because the integer x will also be modified

	if(v.empty()){	// You can't delete elements of a vector with no contents, silly!
		throw std::runtime_error("The vector can't be empty!");	
	}
	x = v.back();	// Integer x is modified, as promised, to the last element of vector v! 
	v.pop_back();	// Deleting the last element of vector v!

	return v;	// Returning vector v with the missing last element.

}


int main(){

	int score = 0;

	//Testing operator>>
	std::vector<int> test_right_stream;
	int out1, out2, out3;
	test_right_stream << 10;
	test_right_stream >> out1;
	if (10 == out1) {
		std::cout<<"Right stream works for single value! +10\n";
	  score += 10;
	}
	else {
		std::cout<<"Right stream fails for single value!\n";
	}

	//Should throw an exception
	try {
		test_right_stream >> out1;
		std::cout<<"Right stream didn't throw an exception when the vector was empty!\n";
	}
	catch (std::runtime_error) {
		std::cout<<"Right stream throws exception when vector is empty! +10\n";
	  score += 10;
	}

	test_right_stream << 20 << 30;
	test_right_stream >> out2 >> out3;
	if (20 == out3 and 30 == out2) {
		std::cout<<"Right stream works when called consecutively! +10\n";
	  score += 10;
	}
	else {
		std::cout<<"Right stream fails when called consecutively!\n";
	}
}
Anyone? D: I am so stumped.
You push 10 on line 35 and then pop it back out on line 36. At this point, the vector is empty. You never push anything else again, so the vector remains empty all the way until line 47, where you try to pop from the vector again, causing an exception.
I don't get that error when running it. Are you sure it's compiling? Not all compilers will accept 'and' without some special compile flags. Try changing 'and' to && on line 57.
I tried changing and to &&, but I get the same error. Hmmm...


Also, I forgot to mention that I can't alter the main because it was provided to me by my professor as a test for the code.

I think my error has to do with the exception I threw. Did I not use it correctly? Because it seems to fail when the exception is thrown.

Does my program stop running when the exception is thrown? I'm not sure how it all works.
The thing I immediately noticed is that you are catching the std::runtime_error by value rather than by reference, which can cause problems (slicing and that), though that shouldn't matter with your program. Its not like you can fix it anyway (if you can't edit main).

However, like with @Lachlan Easton, I get no errors on compiling or running - http://coliru.stacked-crooked.com/a/9ecdec9af3894c3b
What compiler are you using? What settings do you have?
I was running gcc in the windows command prompt. But I just tried running it in CodeBlocks and it worked.

Thanks for the help, guys! It's uber appreciated.
Topic archived. No new replies allowed.