简体   繁体   中英

“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.

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 . You should:

  • Make sure that operator << (ostream&, node::value_type) has been declared before your definition of operator<<
  • If node has a constructor that takes value_type , then it's probably best to make it explicit to avoid unexpected implicit conversions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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