Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++ C++;return语句引发了错误的\u数组\u新的\u长度异常?_C++_Exception_Multidimensional Array_Return - Fatal编程技术网

C++ C++;return语句引发了错误的\u数组\u新的\u长度异常?

C++ C++;return语句引发了错误的\u数组\u新的\u长度异常?,c++,exception,multidimensional-array,return,C++,Exception,Multidimensional Array,Return,这周我一直在做一个矩阵类,遇到了一个让我感到困惑的问题:我的一个函数的return语句抛出了一个坏数组新长度异常 下面是我用来找出这一点的示例代码: Matrix Matrix::operator*(Matrix& mat) { if (this->columns != mat.rows) { //Do code if Matrix can't be multiplied. } else { Matrix re

这周我一直在做一个矩阵类,遇到了一个让我感到困惑的问题:我的一个函数的return语句抛出了一个坏数组新长度异常

下面是我用来找出这一点的示例代码:

Matrix Matrix::operator*(Matrix& mat)
{
    if (this->columns != mat.rows)
    {
        //Do code if Matrix can't be multiplied.
    }
    else
    {
        Matrix result(this->rows, mat.columns);

        //Multiply Matrices together.

        //Print out result to prove the local Matrix works fine.
        //It does.

        try
        {
            return result;
        }
        catch (const exception& e)
        {
            cout << "return exception: " << e.what() << endl;
            return result;  //To be caught again in the test.
        }
    }
}
矩阵::运算符*(矩阵和矩阵)
{
如果(此->列!=材料行)
{
//如果矩阵不能相乘,请编写代码。
}
其他的
{
矩阵结果(此->行,矩阵列);
//将矩阵相乘。
//打印结果以证明局部矩阵工作正常。
//是的。
尝试
{
返回结果;
}
捕获(常量异常和e)
{

cout修复了这个问题,我所要做的就是改变类声明和构造函数中内存的分配方式。

不是所有的路径都是
Matrix::operator*(Matrix&mat)
返回值。您的
矩阵
类缺少用户定义的复制构造函数和赋值运算符。因此,由于内存可能损坏,返回
矩阵
是未定义的行为。此外,您没有或没有在
运算符的一个或多个代码路径中显示返回值*
——同样,未定义的行为r、 哦,如果你真的写了一个复制构造函数和赋值运算符,你不认为这条信息是你在文章中遗漏的最重要的东西吗?因为所有的事情都取决于这些函数是否正确编写?今天的规则是3或5。(移动构造函数和移动赋值两者都有或都没有).但最好是零规则(尽可能使用内置RAII避免写入任何这些内容)。
try
{
    answer = A1 * B1;   //A1 and B1 are able to be multiplied.
}
catch (const exception& e)
{
    cout << e.what() << endl;
}
//Matrix.h

unsigned int rows;
unsigned int columns;

double ** storage = new double*[rows];  //Multidimensional array isn't completely formed, see the constructor.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Matrix.cpp

Matrix::Matrix(unsigned int x, unsigned int y)
:
    rows(x),
    columns(y)
{
    for (unsigned int i = 0; i < rows; ++i) //Completes the formation of the multidimensional array.
        storage[i] = new double[columns];

    for (unsigned int i = 0; i < rows; ++i)
    {
        for (unsigned int q = 0; q < columns; ++q)
        {
            storage[i][q] = 0;  //Initializes each value in the array as 0.
        }
    }
}
Matrix::Matrix(const Matrix& obj)
{
    rows = obj.rows;
    columns = obj.columns;

    for (unsigned int i = 0; i < rows; ++i)
    {
        for (unsigned int q = 0; q < columns; ++q)
        {
            storage[i][q] = obj.storage[i][q];
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

bool Matrix::operator=(Matrix mat)
{
    rows = mat.rows;
    columns = mat.columns;

    for (unsigned int i = 0; i < rows; ++i)
    {
        for (unsigned int q = 0; q < columns; ++q)
        {
            storage[i][q] = mat.storage[i][q];
        }
    }

    return true;
}
try
{
    A1 * B1;    //Removed assignment to answer Matrix.
}
catch (const exception& e)
{
    cout << e.what() << endl;
}