istream Unhandled exception, stack overflow -
i'm new c++, , trying istream work. have class of:
class rat { private: int num; int denom; public: rat(); rat(const int&, const int&); rat(const int&); friend ostream& operator << (ostream&, const rat&); friend istream& operator >> (istream&, const rat&); }; rat::rat(void) { num = 0; denom = 1; } rat::rat(const int &n, const int &d) { num = n; denom = d; simplify(); } rat::rat(const int &n) { num = n; denom = 1; } ostream& operator << (ostream &os, const rat &r1) { os << r1.num; os << "/"; os << r1.denom; return os; } istream& operator >> (istream &is, const rat &r1) { >> r1.num; >> r1.denom; return is; } i have .cpp of:
#include <iostream> #include <conio.h> using namespace std; #include "rats.h" void main() { rat r1(3,4), r2(2,3), r3; system("cls"); cout << "please enter rational number: "; cin >> r3; } my problem occurs whenever comes across "is >> r1.num;" line. gives me error: unhandled exception @ 0x772d15de in ratclass.exe: 0xc00000fd: stack overflow.
again, i'm new, have not learned possible cause yet. appreciated.
looks might fact you're accepting const rat &r1 sending data istream changing r1. can't change constants. not sure if issue that's first obvious thing came mind.
try this:
istream& operator >> (istream &is, rat &r1) { >> r1.num; >> r1.denom; return is; } don't forget change definition in class:
friend istream& operator >> (istream&, rat&);
Comments
Post a Comment