我对 C++ 相当陌生,并且正在尝试让 istream 工作。我有一类:
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)
{
is >> r1.num;
is >> r1.denom;
return is;
}
我还有一个 .cpp:
#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 a rational number: ";
cin >> r3;
}
每当遇到“is >> r1.num;”时,我的问题就会出现。线。它给了我错误:RatClass.exe 中 0x772d15de 处的未处理异常:0xC00000FD:堆栈溢出。
同样,我还很新,所以还没有知道可能的原因是什么。任何帮助表示赞赏。