2016-03-01 23 views
-1

最近我一直在處理Matrix庫以供個人使用。我已經嘗試編寫返回我的Matrix類的代碼(例如運算符重載,轉換和反轉),但會發生奇怪的錯誤。 Visual Studio表示它會拋出一個斷點,可能是由於堆損壞,然後它說了一些關於調試錯誤的信息。我試過了我的代碼的幾個成員,只有返回矩陣的代碼以這種方式失敗。使用C++返回類對象時的錯誤

我正在考慮通過成員的參數傳遞一個預先分配的對象的指針,但這顯然不適用於操作符重載。任何幫助?

下面是錯誤信息:

Windows已經引發了項目Sapphire.exe一個斷點。

這可能是由於堆損壞引起的,這表明 項目Sapphire.exe或它已加載的任何DLL文件中的錯誤。

這也可能是由於用戶按下F12鍵,同時項目 Sapphire.exe具有焦點。「

」調試斷言失敗!

程序:... \桌面\程序\項目藍寶石\調試\項目 Sapphire.exe文件:minkernal \ CRT顯示器\ ucrt的\ src \ appcrt \堆\ debug_heap.cpp 線888

表達: _CrtlsValidHeapPointer(塊)

有關您的程序如何可以導致斷言失敗的信息, 請參閱Visual C++文檔上斷言

(按重試來調試應用程序)

下面的代碼:

Matrix.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(); 
}; 

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 
} 

ProjectSapphire.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; 
} 
+0

如果你不能返回ret,那麼推測Matrix上沒有拷貝構造函數。 –

+0

你可能在你沒有展示的代碼中存在很多錯誤,以及你顯示的錯誤。你應該發佈一個MCVE。 – juanchopanza

+0

你能複製/粘貼確切的錯誤嗎?它可以讓我們更好地幫助你,並讓具有完全相同問題的人容易地找到答案 – Guiroux

回答

3

你的析構函數中的代碼完全是無稽之談。這可能是你問題的根源。

刪除析構函數中的所有代碼(最好是完全移除析構函數)。

快速瀏覽一下,你的代碼看起來不錯。

它會導致未定義的行爲來爲矢量調用析構函數兩次。(成員變量的析構函數在類的析構函數完成後由編譯器調用,如果有的話)。

-1

也許矩陣的複製/移動構造函數被破壞/刪除/私有。

0

永遠不要手動調用析構函數。閱讀關於RAII。所有對象都會自動銷燬。