简体   繁体   English

“在所有控制路径上递归,函数将导致运行时堆栈溢出”,重载<<运算符

[英]“recursive on all control paths, function will cause runtime stack overflow” on overloaded << operator

I have a section of code that seems to have a recursive warning when I compile, any ideas why? 我有一段代码在编译时似乎有递归警告,为什么有什么主意?

ostream& operator << (ostream& out, const node& rhs)
    {
        out << rhs.get_data();
        return out;
    }

It is calling this function: 它正在调用此函数:

node::value_type node::get_data() const
    {
        return data;
    }

This is just a guess, since you haven't posted a self-contained example. 这只是一个猜测,因为您尚未发布独立的示例。 In particular, the definition of node would be very useful. 特别地, node的定义将非常有用。

I think that, for some reason, the compiler is choosing to convert rhs.get_data() into a node , probably using an implicit conversion constructor, rather than selecting an overload of operator<< that takes node::value_type . 我认为,出于某种原因,编译器可能选择使用隐式转换构造函数将rhs.get_data()转换为node ,而不是选择operator<<需要node::value_type的重载。 You should: 你应该:

  • Make sure that operator << (ostream&, node::value_type) has been declared before your definition of operator<< 确保在定义operator<<之前声明了operator << (ostream&, node::value_type)
  • If node has a constructor that takes value_type , then it's probably best to make it explicit to avoid unexpected implicit conversions. 如果node有一个采用value_type的构造函数,那么最好使其explicit以避免意外的隐式转换。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM