在C++中返回类对象时出错
Error in Returning a Class Object in C++
我最近一直在开发一个供个人使用的矩阵库。我尝试编写返回我的 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 的复制/移动构造函数被破坏/删除/私有。
- 在c++中访问int到类对象的映射时出错
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 按基类对象访问派生类资源时出错
- ./main:加载共享库时出错:libopencv_highgui.so.4.0:无法打开共享对象文件:没有这样的文件或
- 在 c++ 的构造函数中分配对象向量时出错
- 从主线程中询问运行对象表 (ROT) 时出错
- 加载共享库时出错:libbsoncxx.so._noabi:无法打开共享对象文件:没有此类文件或目录
- 从 Arduino 中的自定义C++库构造对象时出错
- 在 boost::heap::p riority_queue 中推送结构对象时出错
- 创建 OpenGL 对象期间出错
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 创建类对象时出错
- 尝试将对象用作类的属性时出错
- 擦除矢量中的对象时出错.如何将移动作业运算符添加到我的类中
- 编译创建 Eigen::ThreadPoolDevice 对象的代码时出错
- 尝试使用构造函数创建对象时出错
- 从模板类初始化对象时出错.参考类模板实例化
- 加载共享库时出错:无法在外部硬件上打开共享对象文件:
- 可变大小的对象可能无法初始化 尽管初始化了,但出错
- 删除时出错:表达式必须是指向完整对象类型的指针