Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重载*运算符在多次*操作后调用析构函数时失败 我正在做一个类,为我正在运行的一个测试做矩阵和向量运算,学习更多的C++。该类如下所示: class utlMatrix { private: int num_rows; int num_cols; // number of columns double **data; // array of pointers to the data public: // default constructor, with initialization utlMatrix() : num_rows(0), num_cols(0), data(NULL) {}; // constructor with size utlMatrix(int, int); // destructor ~utlMatrix(); // copy constructor utlMatrix(const utlMatrix&); void copy(const utlMatrix &old); // copy 'old' to 'this' void zero(); // sets all values to zero void fill_rand(); //fills the data with random stuff void print(std::ostream&); // prints the matrix to a file // Operators utlMatrix& operator=(const utlMatrix&); // copies matrices friend utlMatrix operator+(const utlMatrix&, const utlMatrix&); // adds 2 matrices utlMatrix operator*(const utlMatrix&) const; //friend utlMatrix operator*(const utlMatrix&, const utlMatrix&); // multiplies 2 matrices };_C++_Operator Overloading - Fatal编程技术网

重载*运算符在多次*操作后调用析构函数时失败 我正在做一个类,为我正在运行的一个测试做矩阵和向量运算,学习更多的C++。该类如下所示: class utlMatrix { private: int num_rows; int num_cols; // number of columns double **data; // array of pointers to the data public: // default constructor, with initialization utlMatrix() : num_rows(0), num_cols(0), data(NULL) {}; // constructor with size utlMatrix(int, int); // destructor ~utlMatrix(); // copy constructor utlMatrix(const utlMatrix&); void copy(const utlMatrix &old); // copy 'old' to 'this' void zero(); // sets all values to zero void fill_rand(); //fills the data with random stuff void print(std::ostream&); // prints the matrix to a file // Operators utlMatrix& operator=(const utlMatrix&); // copies matrices friend utlMatrix operator+(const utlMatrix&, const utlMatrix&); // adds 2 matrices utlMatrix operator*(const utlMatrix&) const; //friend utlMatrix operator*(const utlMatrix&, const utlMatrix&); // multiplies 2 matrices };

重载*运算符在多次*操作后调用析构函数时失败 我正在做一个类,为我正在运行的一个测试做矩阵和向量运算,学习更多的C++。该类如下所示: class utlMatrix { private: int num_rows; int num_cols; // number of columns double **data; // array of pointers to the data public: // default constructor, with initialization utlMatrix() : num_rows(0), num_cols(0), data(NULL) {}; // constructor with size utlMatrix(int, int); // destructor ~utlMatrix(); // copy constructor utlMatrix(const utlMatrix&); void copy(const utlMatrix &old); // copy 'old' to 'this' void zero(); // sets all values to zero void fill_rand(); //fills the data with random stuff void print(std::ostream&); // prints the matrix to a file // Operators utlMatrix& operator=(const utlMatrix&); // copies matrices friend utlMatrix operator+(const utlMatrix&, const utlMatrix&); // adds 2 matrices utlMatrix operator*(const utlMatrix&) const; //friend utlMatrix operator*(const utlMatrix&, const utlMatrix&); // multiplies 2 matrices };,c++,operator-overloading,C++,Operator Overloading,复制构造函数、赋值运算符和析构函数 // copy constructor utlMatrix::utlMatrix(const utlMatrix &old) { copy(old); } utlMatrix& utlMatrix::operator=(const utlMatrix &old) { copy(old); return *this; } void utlMatrix::copy(const utlMatrix &ol

复制构造函数、赋值运算符和析构函数

// copy constructor
utlMatrix::utlMatrix(const utlMatrix &old) {
    copy(old);
}
utlMatrix& utlMatrix::operator=(const utlMatrix &old)    {
    copy(old);
    return *this;
}
void utlMatrix::copy(const utlMatrix &old)    {
    num_rows = old.num_rows;
    num_cols = old.num_cols;
    data = new float*[num_rows];
    for (int i = 0; i < num_cols; i++)
        data[i] = new float[num_cols];
    for (int i = 0; i < num_rows; i++)
    {
        for (int j = 0; j < num_cols; j++)
            data[i][j] = old.data[i][j];
    }
}
utlMatrix::~utlMatrix()
{
    for (int i = 0; i < num_rows; i++)
        delete [] data[i];
    delete [] data;
}
从屏幕输出中,我知道这是在第二次乘法后调用复制构造函数之后发生的。加法运算符镜像第一个乘法运算符,看起来工作正常,没有异常,复制构造函数和析构函数按预期发生。我找到了两个答案。我检查以确保我的指针没有被复制。复制构造函数使用指向数据的新指针创建新对象。如果我跑步:

int main()
{
utlMatrix A(3,3);
utlMatrix B(2,2);
A.fill_rand();
B.fill_rand();    
B = A;
return 0;
}
调试器显示:

A   {num_rows=3 num_cols=3 data=0x000365c0 ...} utlMatrix
B   {num_rows=3 num_cols=3 data=0x00037c50 ...} utlMatrix
我错过了什么?下面是我实际使用操作符的方式。D=A*B后发生故障

#include "utlMatrix.h"

int main()
{
    // create the first matrices
    utlMatrix A(3,3);
    utlMatrix B(3,3);
    utlMatrix C(3,2);

    // fill them with random numbers
    A.fill_rand();
    B.fill_rand();
    C.fill_rand();

    utlMatrix D = A * B;
    utlMatrix E = A * C;
    utlMatrix F = B * C;
}

错误似乎只有在第二次或第三次调用后才会出现,因为那是矩阵产生非平方输出的时候。这些线路:

for (int i = 0; i < num_cols; i++)
    data[i] = new float[num_cols];
for(int i=0;i

在复制构造函数中,非方形矩阵实际上是作为旧矩阵的大小列的方形矩阵构建的。因为我的案例是一个行多于列的矩阵,所以它试图将数据放入一个不存在的内存位置。按照Igor Tandetnik和Dave S的建议,修复索引并使用SWAP解决了问题。

utlMatrix::copy
中,您有
data=newfloat*[num_rows];对于(int i=0;i
。将
num\u cols
替换为
num\u rows
此外,赋值运算符也不是很“好”,因为它不会删除以前的数据,所以会泄漏。然而,这并不是当前崩溃的问题。
utlMatrix(int,int)
构造函数是什么样子的?它是否分配和归零
数据
数组?您依赖于
操作符中的这一点*
copy
已定义但未声明。你能公布你的实际代码吗?如果你想给我们省事的话,甚至可以先尝试最小化它?你的
copy
方法用
float
s初始化
数据,即使它被声明为指向
double
的指针数组。如果你的非复制构造函数做了同样的事情,这可能是你错误的根源。我正要说同样的事情-我只花了一分钟就发现了它。
#include "utlMatrix.h"

int main()
{
    // create the first matrices
    utlMatrix A(3,3);
    utlMatrix B(3,3);
    utlMatrix C(3,2);

    // fill them with random numbers
    A.fill_rand();
    B.fill_rand();
    C.fill_rand();

    utlMatrix D = A * B;
    utlMatrix E = A * C;
    utlMatrix F = B * C;
}
for (int i = 0; i < num_cols; i++)
    data[i] = new float[num_cols];