gpt4 book ai didi

c++ - 重载>>输入矩阵(类)的功能不起作用

转载 作者:行者123 更新时间:2023-11-28 07:01:50 25 4
gpt4 key购买 nike

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

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

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

最佳答案

您永远不会在您的 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();

关于c++ - 重载>>输入矩阵(类)的功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com