简体   繁体   English

为什么编译器报告“operator<< and operator>> recursive on all control path will cause stack overflow”?

[英]Why does the compiler report "operator<< and operator>> recursive on all control paths will cause stack overflow "?

#include <iostream>

class fraction {
    int n, d;
public:
    fraction(){}
    fraction(int n, int d) : n(n), d(d) {}
    int getter() { return n, d; }

    friend std::istream& operator>>(std::istream& stream, const fraction& a) {
        stream >> a;
        return stream;
    }

    friend std::ostream& operator<<(std::ostream& stream, const fraction& a) {
        stream << a;
        return stream;
    }

    friend fraction operator*(const fraction& a, const fraction& b) {
        int pN = a.n * b.n;
        int pD = b.n * b.d;
        return fraction(pN, pD);
    }   
};

int main()
{
    fraction f1;
    std::cout << "Enter fraction 1: ";
    std::cin >> f1;

    fraction f2;
    std::cout << "Enter fraction 2: ";
    std::cin >> f2;

    std::cout << f1 << " * " << f2 << " is " << f1 * f2 << '\n'; // note: The result of f1 * f2 is an r-value

    return 0;
}

Compiling error says:编译错误说:

operator<< and operator>> recursive on all paths, function will cause a stack overflow

I don't know what this means.我不知道这是什么意思。 What does it mean by recursive on all paths and which function is gonna cause a stack overflow?在所有路径上递归是什么意思,哪个函数会导致堆栈溢出?

When you run:当你运行时:

stream >> a;

You are calling the same function you are running, ie friend std::istream& operator>>(std::istream& stream, const fraction& a) .您正在调用正在运行的相同函数,即friend std::istream& operator>>(std::istream& stream, const fraction& a)

So you will be calling yourself ( recursion ) again, and again, and again... without an end.所以你会一次又一次地叫自己(递归),一次又一次......没有结束。 This, in turn, means that the memory assigned to the stack will be exhausted at some point (because each frame takes some space) and it will cause a stack overflow .反过来,这意味着分配给堆栈的内存将在某个时候耗尽(因为每个帧都占用一些空间)并且会导致堆栈溢出

Instead, you have to do something with the fraction argument a , most likely refer to an and ad .相反,您必须对fraction参数a做一些事情,最有可能引用anad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “在所有控制路径上递归,函数将导致运行时堆栈溢出”,重载&lt;&lt;运算符 - “recursive on all control paths, function will cause runtime stack overflow” on overloaded << operator 复制和交换习语警告:在所有控制路径上递归,函数将导致运行时堆栈溢出 - Copy&Swap idiom warning : recursive on all control paths, function will cause runtime stack overflow 当operator &lt;在所有路径上递归时,Visual Studio 2008运行时堆栈溢出警告 - Visual Studio 2008 run-time stack overflow warning when operator< recursive on all paths 为什么cin提取操作符导致段错误? - Why does the cin extraction operator cause a segfault? 编译器显示“在所有控制路径上递归”,但 function 配备了一个终端案例 - Compiler displays “recursive on all control paths” but the function is equipped with a end case 为什么编译器会两次调用 - >运算符 - Why does the compiler invoke the -> operator twice 运算符ThisClass()导致堆栈溢出 - operator ThisClass() causing stack overflow 重载&lt;&lt;运算符时堆栈溢出 - Stack overflow when overloading << operator 警告C4715:“ operator ==”:并非所有控制路径都返回值 - warning C4715: 'operator==' : not all control paths return a value 为什么std :: regex_iterator会导致堆栈溢出这个数据? - Why does std::regex_iterator cause a stack overflow with this data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM