How to properly overload the << operator for an ostream?

前端 未结 5 1243
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 04:54

I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in b

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 05:35

    To add to Mehrdad answer ,

    namespace Math
    {
        class Matrix
        {
           public:
    
           [...]
    
    
        }   
        std::ostream& operator<< (std::ostream& stream, const Math::Matrix& matrix);
    }
    

    In your implementation

    std::ostream& operator<<(std::ostream& stream, 
                         const Math::Matrix& matrix) {
        matrix.print(stream); //assuming you define print for matrix 
        return stream;
     }
    

提交回复
热议问题