错误:当我流到cout时,'operator <'不匹配

Moh*_*taq 2 c++

我正在创建一个Matrix类,我正在重载所有的基本运算符.例如:

class Matrix {
    Matrix operator<(const float& ); // returns a Matrix with
                                     // entries 0 or 1 based on
                                     // whether the element is less than
                                     // what's passed in.


};
Run Code Online (Sandbox Code Playgroud)

我还写了一个流媒体运营商:

ostream &operator<<(ostream&cout,const Matrix &M){
    for(int i=0;i<M.rows;++i) {
        for(int j=0;j<M.columns;++j) {
            cout<<M.array[i][j]<<"  ";
        }
        cout<<endl;
    }
    return cout;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用这些时:

int main() {
     Matrix M1;
     cout << M1 < 5.8;
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

错误:' operator<'in' operator<<((* & std::cout), (*(const Matrix*)(& m))) < 5.7999999999999998e+0' 不匹配

这个错误是什么意思?

Que*_*onC 6

左流操作符的<<优先级高于比较运算符<.

所以...

cout << M1 < 5.8

相当于

(cout << M1) < 5.8

http://en.cppreference.com/w/cpp/language/operator_precedence


PS.这种行为是愚蠢的,但我们因历史原因而坚持下去.最初的意图<<是数学运算(这个优先级有意义),而不是流式传输.