[英]Error: no match for operator << in std::cout (I have already overloaded the << operator)
[英]error: no match for ‘operator<’ when I stream to cout
我正在創建一個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.
};
我還寫了一個流運算符:
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;
}
但是,當我嘗試使用這些時:
int main() {
Matrix M1;
cout << M1 < 5.8;
}
我收到此錯誤:
錯誤:不對應的'
operator<
'中'operator<<((* & std::cout), (*(const Matrix*)(& m))) < 5.7999999999999998e+0
'
這個錯誤是什么意思?
左流運算符<<
優先級高於比較運算符<
優先級。
所以...
cout << M1 < 5.8
相當於
(cout << M1) < 5.8
http://en.cppreference.com/w/cpp/language/operator_precedence
PS。 這種行為是愚蠢的,但由於歷史原因,我們一直堅持下去。 <<
的最初意圖是進行數學運算(在此優先級有意義),而不是流式傳輸。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.