运算符重载时未处理<<异常
Unhandled exception when overloading << operator
我试图超载&lt;&lt;一个新的整数类的操作员检测操作溢出及以下是我的代码部分:
class NewInteger{
private:
int num;
public:
NewInteger();
NewInteger(int);
friend std::ostream &operator <<(std::ostream& out, const NewInteger& rhs);
NewInteger operator+ (NewInteger);
.../ many other member functions/
}
实现是:
NewInteger NewInteger::operator+(NewInteger n)
{
int a = this->getValue();
int b = n.getValue();
if (a > 0 && b > max - a) {
throw std::exception();
std::cout << "studpid" << std::endl;
}
if (a < 0 && b < min - a) {
throw std::exception();
}
return NewInteger(a + b);
}
std::ostream & operator<<(std::ostream & out, const NewInteger& rhs)
{
out << rhs;
return out;
}
在main.cpp中,我尝试通过运行来测试代码:
NewInteger n7(4);
NewInteger n8(5);
std::cout << n7.operator+(n8) << std::endl;
代码构建正常,当我在Visual Studio 2015上运行它时,它会导致该程序关闭而不会出现致命错误。因此,当我调试代码时,它给了我:`
Exception thrown at 0x00C43B49 in NewInteger.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00192F90)
,断裂点出现在实施操作员&lt;&lt;中。但是我无法弄清楚我应该尝试在实施中捕获哪些例外。
有人可以告诉我这是什么原因吗?
std::ostream & operator<<(std::ostream & out, const NewInteger& rhs)
{
out << rhs;
return out;
}
第一行调用out
和rhs
上的operator<<
- 这是您定义的功能。您有无限的递归。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 为什么异常不退出程序?
- <<操作员在下面的行中工作
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- log4cxx访问异常,使用<<运算符和宏
- 重载& lt; & lt;用于打印自定义异常的操作符