It appears what you're doing is overloading the global >> operator.
The alternative would be to make it a member of Chain, in which case, as you said the signature will change, since you would then only need to pass the istream &.
I could be wrong, but I think overloading the non-member form as you've done is the preferred way.
In your latest code, I don't see where you're storing anything in "stop" or "letter"
I would be tempted to just read a string and then process it, also I'm never sure about precedence rules, so parentheses:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
istream& operator>>(istream& in_stream, Chain & input)
{
string stop;
int temp_input = 0;
while ((in_stream >> stop) && (stop != "stop"))
{
try
{
temp_input = stoi(stop);
input.Add(temp_input);
}
catch(const invalid_argument &ia)
{
cerr << "Invalid argument: " << ia.what() << endl;
}
catch(const out_of_range &oor)
{
cerr << "Out of Range error: " << oor.what() << endl;
}
}
return in_stream;
}
|
I'm just trying to be fancy with the try-catch block, i'm not sure it makes sense for what you're doing, and I've typed this without verification so, take with a pinch of salt
Addendum/Errata: I may have programmed an infinite loop up there, you'll need to break out of the while loop from within the catch statements; otherwise, you'd be stuck in the loop unless instream >> fails or the user types "stop"