내가 작업의 오버 플로우를 감지하고 아래 코드의 내 부분입니다 새 정수 클래스의 < < 연산자를 오버로드하는 것을 시도하고있다 :처리되지 않은 예외 과부하 << 연산자
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/
}
구현은 다음과 같습니다
가 MAIN.CPP에서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;
}
, 나는 실행하여 코드를 테스트하려고 :
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)
하고 브레이크 포인트는 바로 연산자 < <의 구현에 나타납니다. 그러나 구현시 어떤 예외를 잡아야하는지 파악할 수 없습니다.
누군가이 문제의 원인을 알려주실 수 있습니까? 당신이 정의하고있는 기능입니다 -
은
대기 ... 무한 재귀'연산자 <<'에 대한 * 의도? 왜냐하면 그것은 * 정확히 * 코딩 한 것입니다. – WhozCraig
이 "예외"는 C++ 예외가 아니므로 catch로 catch 할 수 없습니다. – aschepler