重载>>输入矩阵(类)的函数不起作用

Function to overload >> to input a matrix (class) isn't working

本文关键字:gt 不起作用 函数 输入 重载      更新时间:2023-10-16

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

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

你知道我做错了什么吗?完整代码在这里

编辑:忘了说,我包含赋值操作符是因为它有相同(或类似)的问题。

您从未在operator>>过载中更新mat.size,因此尽管您读取了4个值,但矩阵认为它是空的并且没有打印出任何内容。

同样,非常重要的是,如果传递给operator>>过载的矩阵已经有数据,那么您将泄漏内存,因为您没有释放该数据,并且您将新指针分配给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&)成员函数,这是一个有用的东西,有很多原因。

    return is;
}

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

赋值操作符的错误在这里:

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

声明一个新的局部变量size,并更新该变量。这意味着this->size永远不会更新。您不想声明一个新的size,您只想更新成员变量,因此将上述更改为:

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