【问题标题】:Function to overload >> to input a matrix (class) isn't working重载>>输入矩阵(类)的功能不起作用
【发布时间】:2014-04-15 13:17:08
【问题描述】:

所以,我写了这个类,它看起来有点像这样:

class matrix
{
    // Friends
    friend ostream & operator << (ostream &os, const matrix &mat);
    friend istream & operator >> (istream &is, matrix &mat); 
    private:
        double *mdata;
        int rows, columns, size;

后来我写了:

// Assignment operator
    public:
        matrix & operator=(const matrix &m)  {
            if(&m == this) {
                return *this; // no self assignment
            }


    // First delete this object's array
        delete[] mdata;
        columns=0;
        rows=0;
        mdata=0;
        int size=0;
        // Now copy size and declare new array
        size=m.getcols()*m.getrows();
        if(size>0) {
            mdata=new double[size];
            // Copy values into new array
            for(int i=0;i<size;i++) {
                mdata[i] = m.mdata[i];
            }
        }
        columns = m.getcols();
        rows = m.getrows();
        return *this; // Special pointer
    }

我在课外有这个:

ostream & operator << (ostream &os, const matrix &mat) {
    // Code goes here
    os << "\n";
    int j = 1;
    for (int i=0; i < mat.size; i++) {
        os << mat.mdata[i] << " ";
        if (i+1 == j*mat.getcols()) {
            os << "\n";
            j = j + 1;
        }
    }
    os << "\n";
    os << "Wolfram|Alpha code:\n[[";
    j = 1;
    for (int i=0; i < mat.size; i++) {
        os << mat.mdata[i];
        if (i+1 != j*mat.getcols()){
            os << ",";
        }
        if (i+1 == j*mat.getcols()) {
            if (i+1 != mat.size) {
                os << "],[";
            }
            else {
                os << "]";
            }
            j = j + 1;
        }
    }
    os << "] \n";
    return os;
}

istream & operator >> (istream &is, matrix &mat) { 
    is >> mat.rows >> mat.columns;
    int size(mat.rows*mat.columns);
    if(size>0) {
        cout << "Enter " << size << " values (top row, second row... last row - left to right)" << endl;
        mat.mdata=new double[size];
        // Copy values into new array
        for(int i=0;i<size;i++) {
            is >> mat.mdata[i];
        }
    }
    return is;
}

但是在运行代码时(在 main 中):

cout << "Enter the rows and columns of a custom Matrix, row then column:" << endl;
matrix custom;
cin >> custom;
cout << custom;
cout << custom.getrows() << endl;

我没有打印出任何值...

Enter the rows and columns of a custom Matrix, row then column:
Default matrix constructor called
2
2
Enter 4 values (top row, second row... last row - left to right)
1 2 3 4


Wolfram|Alpha code:
[[] 
2
Destructor called

关于我做错了什么有什么想法吗?完整代码here

编辑: 忘了说,我包括了赋值运算符,因为它有相同(或相似)的问题。

【问题讨论】:

    标签: c++ class oop operator-overloading istream


    【解决方案1】:

    您永远不会在您的 operator&gt;&gt; 重载中更新 mat.size,因此尽管您读取了 4 个值,但矩阵认为它是空的并且什么也没有打印出来。

    另外,非常重要的是,如果传递给 operator&gt;&gt; 重载的矩阵已经有数据,那么您将泄漏内存,因为您没有释放该数据,而是为 mat.mdata 分配了一个新指针

    你可以这样做:

    istream & operator >> (istream &is, matrix &mat) { 
        int rows, columns;
        is >> rows >> columns;
        if (!is)
            return is;  // couldn't read rows and cols
        matrix tmp(rows, columns);
        if(tmp.size>0) {
            cout << "Enter " << tmp.size << " values (top row, second row... last row - left to right)" << endl;
    

    注意不要在这里分配新数组,这是在上面的构造函数中完成的。

           // Copy values into new array
            for(int i=0;i<tmp.size;i++) {
                is >> tmp.mdata[i];
            }
        }
        if (is)   // read all values successfully
            mat = tmp;
    

    这要求您的赋值运算符正确工作,因此现在是确保这一点的好时机!另一种选择是交换:

            mat.swap(tmp);
    

    这需要一个正确的matrix::swap(matrix&amp;) 成员函数,这是一个有用的东西,有很多原因。

        return is;
    }
    

    请注意,我进行了错误检查以确保您确实从流中读取了预期的数据。

    您的赋值运算符中的错误在这里:

            int size=0;
            // Now copy size and declare new array
            size=m.getcols()*m.getrows();
    

    您声明一个名为size 的新局部变量,并更新该变量。这意味着this-&gt;size 永远不会更新。你不想声明一个新的size,你只想更新成员变量,所以把上面的改成:

            // Now copy size and declare new array
            size=m.getcols()*m.getrows();
    

    【讨论】:

    • 感谢您的详细回答,我会在今晚晚些时候回到我的计算机时检查它,看看它是否解决了我的问题并将其标记为已接受:)
    • 哦,是的,我忘记了我什至在矩阵中包含了一个大小变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多