在C++中返回类对象时出错

Error in Returning a Class Object in C++

本文关键字:对象 出错 返回 C++      更新时间:2023-10-16

我最近一直在开发一个供个人使用的矩阵库。我尝试编写返回我的 Matrix 类的代码(例如运算符重载、转换和反转),但发生了奇怪的错误。Visual Studio说它抛出了一个断点,可能是由于堆损坏,然后它说了一些关于调试错误的信息。我已经尝试了代码的几个成员,只有返回矩阵的代码以这种方式失败。

我正在考虑通过成员的参数传递预分配对象的指针,但这显然不适用于运算符重载。有什么帮助吗?

以下是错误消息:

Windows 在 Project Sapphire.exe 中触发了一个断点。

这可能是由于堆损坏,这表明 蓝宝石项目.exe或其已加载的任何 DLL。

这也可能是由于用户在项目时按 F12 蓝宝石.exe有重点。

"调试断言失败!

程序

:...\桌面\程序\项目蓝宝石\调试\项目 Sapphire.exe file: minkernal\crts\ucrt\src\appcrt\heap\debug_heap.cpp 888路

表达式:_CrtlsValidHeapPointer(块)

有关程序如何导致断言失败的信息, 请参阅有关断言的可视化C++文档

(按重试以调试应用程序)

代码如下:

矩阵.h

#pragma once
#include "stdafx.h"
#include <vector>
class Matrix
{
private:
std::vector<std::vector<float>> data;
public:
std::vector<int> getSize();
//Operator overloads
std::vector<float> operator[](int index);
Matrix operator+(const Matrix& fuz);
//Constructor and destructor
Matrix(int dim1, int dim2);
~Matrix();
};

矩阵.cpp

    #include "stdafx.h"
#include "Matrix.h"
std::vector<int> Matrix::getSize() //This seems to work well
{
    std::vector<int> size(2);
    size[0] = data.size();
    size[1] = (data.size() >= 1 ) ? data[0].size() : 0; //Makes sure to not try to access an empty vector
    return size;
}
//Operator Overloads
std::vector<float> Matrix::operator[](int index)
{
    return data.at(index); //Does the same thing as data[], from what I can tell
}
Matrix Matrix::operator+(const Matrix& fuz)
{
    if (( fuz.data.size() == this->getSize()[0]) && (fuz.data[0].size() == this->getSize()[1])) //Makes sure that the matrix dimensions align
    {
        Matrix ret = *this; //The return matrix. Starts out as a copy of this matrix
        for (int i = 0; i < this->getSize()[0]; i++)
        {
            for (int j = 0; j < (int) this->getSize()[1]; j++)
            {
                ret[i][j] += fuz.data[i][j]; //Goes through and adds the two matrices
            }
        }
        return ret; //Error happens HERE
    }
    return Matrix(1, 1); //Otherwise, return a scalar
}
//Constructor and Destructor
Matrix::Matrix(int dim1, int dim2)
{
    data.resize(dim1); //Allocate the first dimension of the matrix
    for (int i = 0; i < dim1; i++) //Allocate the second dimension of the matrix
        data[i].resize(dim2);
}
Matrix::~Matrix()
{
    for (int i = 0; i < this->getSize()[0] - 1; i++) //Destruct each vector inside of the vector
        data[i].~vector();
    data.~vector(); //Delete the actual data
    delete[] & data; //Finally, delete data
}

项目蓝宝石.cpp

    // Project Sapphire.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
//#include "Node.h"
#include "Matrix.h"
//namespace SWF = System::Windows::Forms;

int main()
{
    Matrix test(2,3);
    int size[] = { test.getSize()[0], test.getSize()[1] };
    test[0][0] = 1;
    test[0][1] = 2;
    test[0][2] = 3;
    test[1][0] = 4;
    test[1][1] = 5;
    test[1][2] = 6;
    Matrix foo = test;
    Matrix deet = foo + test;
    return 0;
}

析构函数中的代码完全是胡说八道。这可能是您问题的根源。

删除析构函数

中的所有代码(最好完全删除析构函数)。

一看,除此之外,您的代码看起来还可以。

它会导致未定义的行为为向量调用析构函数两次。(成员变量的析构函数在类的析构函数(如果有)完成后由编译器调用)。

永远不要手动调用析构函数。阅读有关 RAII 的信息。所有对象都会自动销毁。

也许 Matrix 的复制/移动构造函数被破坏/删除/私有。