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.
#include <iostream>
#include <vector>
#include <stdexcept>
usingnamespace 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";
}
}
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.
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).