C++ _ASSERTE(_BLOCK_TYPE_IS VALID(pHead->;nBlockUse))错误

C++ _ASSERTE(_BLOCK_TYPE_IS VALID(pHead->;nBlockUse))错误,c++,C++,我试图弄清楚为什么我的程序在运行时失败。到目前为止,当我运行我的程序时,它在我身上失败了。我调试了错误,它将我带到dbgdel.cpp。第32行“_ASSERTE(_BLOCK_TYPE_有效(pHead->nBlockUse));”。我一直在寻找答案,但毫无结果,但这与我的内存泄漏有关 这是我的标题: #include <iostream> using namespace std; namespace project { #ifndef MATRIX_H #define MATRI

我试图弄清楚为什么我的程序在运行时失败。到目前为止,当我运行我的程序时,它在我身上失败了。我调试了错误,它将我带到dbgdel.cpp。第32行“_ASSERTE(_BLOCK_TYPE_有效(pHead->nBlockUse));”。我一直在寻找答案,但毫无结果,但这与我的内存泄漏有关

这是我的标题:

#include <iostream>
using namespace std;
namespace project
{
#ifndef MATRIX_H
#define MATRIX_H

typedef int* IntArrayPtr;
class Matrix
{
public:
    friend ostream& operator<<(ostream& out, Matrix& object);
    //friend istream& operator>>(istream& in, Matrix& theArray);
    //Default Constructor
    Matrix();
    Matrix(int max_number_rows, int max_number_cols, int intial_value);
    //Destructor
    ~Matrix();
    //Copy Constructor
    Matrix(const Matrix& right_side);
    //Assignment Operator
    Matrix& operator=(const Matrix& right_side);

    void Clear();
    int Rows();
    int Columns();
    bool GetCell(int x,int y, int& val);
    bool SetCell(int x,int y, int val);
    int getNumber(int r, int c);
    //void Debug(ostream& out);
private:
    int initialVal;
    int rows;
    int cols;
    IntArrayPtr *m;
};
#endif
}
#包括
使用名称空间std;
命名空间项目
{
#ifndef矩阵
#定义矩阵
typedef int*IntArrayPtr;
类矩阵
{
公众:
friend ostream&operator(istream&in,Matrix&theArray);
//默认构造函数
矩阵();
矩阵(整数最大数行、整数最大数列、整数初始值);
//析构函数
~Matrix();
//复制构造函数
矩阵(常数矩阵和右侧);
//赋值运算符
矩阵和运算符=(常数矩阵和右侧);
无效清除();
int行();
int列();
bool-GetCell(整数x、整数y、整数和值);
布尔设置单元(整数x,整数y,整数val);
int getNumber(int r,int c);
//无效调试(ostream&out);
私人:
int initialVal;
int行;
int cols;
IntArrayPtr*m;
};
#恩迪夫
}
我的实现文件:

#include <iostream>
#include "Matrix.h"
 using namespace project;
using namespace std;

namespace project
{
Matrix::Matrix()
{
typedef int* IntArrayPtr;
IntArrayPtr *m = new IntArrayPtr[1];
for(int i = 0; i < 1; i++)
    m[i] = new int[1];

for(int r = 0; r < 1; r++)
    for(int c = 0; c < 1;c++)
        m[r][c] = 0;
}
Matrix::Matrix(int max_number_rows, int max_number_cols, int initial_value)
{
initialVal = initial_value;
rows = max_number_rows;
cols = max_number_cols;
IntArrayPtr *m = new IntArrayPtr[rows];
for(int i = 0; i < rows; i++)
    m[i] = new int[cols];

for(int r = 0; r < max_number_rows; r++)
    for(int c = 0; c < max_number_cols;c++)
        m[r][c] = initial_value;

}
Matrix::~Matrix()
{
for(int i = 0; i <= rows; i++)
    delete [] m[i];
delete [] m;
}
void Matrix::Clear()
{
for(int r = 0; r < rows; r++)
    for(int c = 0; c < cols;c++)
        m[r][c] = initialVal;
}

int Matrix::Rows()
{
return rows;
}

int Matrix::Columns()
{
return cols;
}

bool Matrix::GetCell(int x,int y, int& val)
{
if(x < rows || y < cols)
    return false;
else
{
    val =   m[x - 1][y - 1];
    return true;
}
}

bool Matrix::SetCell(int x, int y, int val)
 {
if(x < rows || y < cols)
    return false;
else
{
    m[x - 1][y - 1] = val;
    return true;
}
}
int Matrix::getNumber(int r, int c)
{
return m[r][c];
}

ostream& operator<<(ostream& out, Matrix& object)
{

for(int r = 0; r < object.rows; r++)
{
    for(int c = 0; c < object.cols; c++)
    {
        out << " " << object.m[r][c];
    }
out << endl;
}
return out;
}

Matrix& Matrix::operator=(const Matrix& right_side)
{
if (this != &right_side)
{
    rows = right_side.rows;
    cols = right_side.cols;
    delete[] m;
    IntArrayPtr *m = new IntArrayPtr[rows];
    for(int i = 0; i < rows; i++)
        m[i] = new int[cols];

    for(int r = 0; r < rows; r++)
        for(int c = 0; c < cols;c++)
            m[r][c] = right_side.initialVal;
}
return *this;
}

/*
void Matrix::Debug(ostream& out)
{
out << "Number of rows = " << rows << endl;
out << "Number of columns = " << cols << endl;
out << "Initializer = " << initialVal << endl;
out << "Current contents of the matrix: " << endl;
out << m << endl;
}
*/
/*
istream& operator >>(istream& in, Matrix& theArray)
{
in >> 


}

void interfaceMatrix()
{
int userChoice, rows, columns, value;

cout << "Default matrix or custom(1 for default, 0 for custom): ";
cin >> userChoice;
    if (userChoice == 1)
    {
        Matrix object;
        return object;
    }
    else if(userChoice == 0)
    {
        cout << "Enter number of rows: ";
        cin >> rows;
        cout << "Enter number of columns: ";
        cin >> columns;
        cout << "Enter initial value of each element: ";
        cin >> value;
        if(rows <= 0 || columns <= 0)
        {
            cout << "Invalid input." << endl;
            exit(1);
        }
        else
        {
            Matrix object(rows, columns, value);
            return object;
        }
    }
    else
    {
        cout << "Invalid choice." << endl;
        exit(1);
    }

}
*/
}
#包括
#包括“矩阵h”
使用名称空间项目;
使用名称空间std;
命名空间项目
{
矩阵::矩阵()
{
typedef int*IntArrayPtr;
IntArrayPtr*m=新IntArrayPtr[1];
对于(int i=0;i<1;i++)
m[i]=新整数[1];
对于(int r=0;r<1;r++)
对于(int c=0;c<1;c++)
m[r][c]=0;
}
矩阵::矩阵(整数最大数行、整数最大数列、整数初始值)
{
初始值=初始值;
行数=最大行数;
cols=最大数量cols;
IntArrayPtr*m=新IntArrayPtr[行];
对于(int i=0;i对于(int i=0;i您正在分配
行数
行数,但取消分配
行数+1
行数。 检查析构函数。两个问题:

  • 您没有将任何值分配给对象的“m”成员,而是分配给名为“m”的局部变量

  • 你从i=0循环到i,这是过度分配。我不认为“IntArrayPtr*m=new IntArrayPtr[rows];”是你认为的意思。谢谢。我明白了。我是个白痴:(@user3112739)不要删除问题的内容。单击答案上的绿色复选标记表示感谢。