Overloading operator<<: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’

后端 未结 2 1172
一个人的身影
一个人的身影 2021-01-23 21:40

Searching for the title of this question gives me a number of people quoting the same error, but under different circumstances, and unfortunately the answers there provided are

2条回答
  •  别那么骄傲
    2021-01-23 22:05

    Change this:

    std::ostream& operator<< ( std::ostream&, Vector< U, M >& );
    

    to this:

    std::ostream& operator<< ( std::ostream&, const Vector< U, M >& );
    //                                        ^^^^^
    

    The compiler is telling you that C++ will not let you bind a temporary Vector such as u + v to a non-const Vector&.

    And you don't modify that Vector, so it should be const to begin with.

提交回复
热议问题