重载" >> " 程序崩溃
Overloading " >> " Crashed the program
#include <iostream>
#include <sstream>
using namespace std;
class Rupee{
private:
int number;
int number2;
int number3;
public:
Rupee()
{
}
friend ostream &operator<<( ostream &os, const Rupee &e );
friend istream &operator>>( istream &is, const Rupee &e );
};
ostream& operator << (ostream & os ,const Rupee &e)
{
os<<endl << e.number << endl;
os << e.number2<< endl;
os << e.number3<< endl;
return os;
}
istream& operator >> (istream & is ,const Rupee &e)
{
is>>e.number>>e.number2>>e.number3;
return is;
}
int main()
{
Rupee o1;
cin >> o1;
cout << o1;
}
你好,我正在尝试使操作员<<
重载。当IM运行程序时,程序不为" << "
运行,但适用于" >> "
。我在哪里做错?我是否使用正确的iStream和Ostream类来用于超载功能?
评论者提到的编译问题是将const引用传递给>>运算符。值被读取到变量中,因此使其构成导致编译器错误,应删除const关键字:
friend istream &operator >> (istream &is, Rupee &e);
istream& operator >> (istream & is, Rupee &e)
进行了这些更改后,程序编译并为我运行良好,Visual Studio 2015在这里。但是,您可能需要暂停出口上的程序以读取输出:
cin.ignore();
cin.get();
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 程序崩溃并显示"std::out_of_range"错误
- CoInitialize()在单独的线程上崩溃而不返回
- 使用调试/崩溃报告将应用程序部署到客户端
- 为什么所有C++编译器都会崩溃或挂起此代码
- EASTL矢量<向量<int>>连续的
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 为什么我的多线程作业队列崩溃
- ExtractIconEx:可以工作,但偶尔会崩溃
- 为什么引用传递会导致此崩溃(C++)
- 试图创建流或fopen时程序崩溃
- 类对象数组的问题会导致崩溃
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 为什么要增加导致崩溃的指针
- 在虚幻引擎中删除NXOpen对象时崩溃
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Visual Studio在尝试读取resource.txt文件时崩溃