小编Moh*_*taq的帖子

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

我正在创建一个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' 不匹配

这个错误是什么意思?

c++

2
推荐指数
1
解决办法
74
查看次数

调用默认构造函数

这个程序给出了正确的输出,但我无法理解.如何在声明对象时调用默认构造函数?

#include <iostream>
using namespace std;
class GuessMe {
private:   
    int *p;
public:
    GuessMe(int x=0) 
    {
        p = new int;
    }
    int GetX() 
    {
        return *p;
    }
    void SetX(int x) 
     {
        *p = x;
     }
    ~GuessMe() 
     {
        delete p;
     }
};

 int main() {
    GuessMe g1;
    g1.SetX(10);
    GuessMe g2(g1);
    cout << g2.GetX() << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
66
查看次数

标签 统计

c++ ×2