编译动态矩阵时出现file.exe错误

时间:2015-04-30 12:46:50

标签: c++ class dynamic matrix

我正在尝试使用类创建动态矩阵。我做了这个代码。检查其他代码,它似乎是正确的。编译时我没有收到任何错误消息,除了运行这部分代码时程序中间的file.exe错误。它说file.exe已停止工作。

有人能看到这段代码有什么问题吗?

标题文件

#pragma once
#include <iostream>
class Matrix {
    unsigned int nRows, nColumns;
    double **board;
public:
    Matrix();
    Matrix(unsigned int nRows, unsigned int nColumns);
    ~Matrix();
};

的.cpp

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

Matrix::Matrix()
: nRows(0)
, nColumns(0)
, board(nullptr) 
{}
Matrix::Matrix(unsigned int nRows, unsigned int nColumns)
: nRows(nRows)
, nColumns(nColumns)
, board(nullptr)
{   

    board = new double*[nRows];
    for (int i = 0; i < nRows; ++i)
    {
    board[i] = new double[nColumns];
    }


    for (int i =0; i<nRows; ++i)
    {
        for(int j=0; j<nColumns; ++j)
        {
            board[i][j]=0.0;
        }
    }
}
Matrix::~Matrix() 
{
  for (int i = 0; i < nRows; ++i){
  delete [] board[i];
  delete [] board;
  board = 0;

  }
}

主要

#include <iostream>
#include "matrix.h"

int main(){
Matrix A = Matrix(5,5);
}

我正在运行Visual Studios 2010

编辑: 我已经调试过,问题似乎在默认构造函数和我正在尝试使用的构造函数中。在.cpp的第13行和第17行,它尝试访问此行: free.c中的retval = HeapFree(_crtheap, 0, pBlock);

调试器状态 :加载'C:\ Windows \ SysWOW64 \ ntdll.dll',找不到或打开PDB文件0xC0000005 :访问冲突读取位置0x00000004

1 个答案:

答案 0 :(得分:0)

您正在复制Matrix,但您没有有效的复制构造函数。将您的代码更改为以下内容:

#include <iostream>
#include "matrix.h"

int main(){
   Matrix A(5,5);
   return 0;
}

或者,您可以添加复制构造函数:

Matrix::Matrix(const Matrix& other) : 
    Matrix(other.nRows, other.nColumns)
{
    for (int i =0; i<nRows; ++i)
    {
        for(int j=0; j<nColumns; ++j)
        {
            board[i][j]=other.board[i][j];
        }
    }
}

你采取的方法非常'C'。如果我正在实现矩阵类,我会使用标准的c ++容器,可能是数组或向量,具体取决于具体情况。

这是矩阵的有效实现。我没有真正考虑它或测试,并且可能有一百万个其他在线实现更好:

#include <vector>
#include <iostream>
#include <stdexcept>
#include <iomanip>

using namespace std;

class Matrix {
public:
   Matrix() : nRows(0), nColumns(0), maxSize(0) {}
   Matrix(size_t r, size_t c) : 
      nRows(r), nColumns(c), cont(r*c, 0), maxSize(r*c)
   {      
   }

   Matrix(const Matrix& other) : 
      nRows(other.nRows), 
      nColumns(other.nColumns),
      cont(other.cont.begin(), other.cont.end())
   {
   }

   void grow(size_t r, size_t c) {
      vector<double> growth(r*c, 0);
      for (size_t i=0; i<nRows; ++i) {
         for (size_t j=0; j<nColumns; ++j) {
            size_t indexNew = convertToIndex(j, i, c, r*c);
            size_t indexOld = convertToIndex(j, i);
            growth[indexNew] = cont[indexOld];
         }
      }
      nRows = r;
      nColumns = c;
      maxSize = r * c;
      cont = growth;
   }

   double get(size_t row_i, size_t col_i) const {
      return cont[convertToIndex(col_i, row_i)];
   }

   void set(size_t row_i, size_t col_i, double val) {
      cont[convertToIndex(col_i, row_i)] = val;
   }

   void printHeader() const {
      for (size_t col = 0; col < nColumns; ++col) {
         size_t width = 5;
         if (col == 0) { width = 10; }
         cout << setw(width) << col + 1;
      }
      cout << endl;
   }

   void printRow(size_t r) const {
      cout << fixed << setprecision(1);
      for (size_t col = 0; col < nColumns; ++col) {
         double d = get(r, col);
         cout << setw(5) << d;
      }
   }

   void print() const {
      if (nRows == 0 || nColumns == 0) { return; }
      printHeader();
      for (size_t row = 0; row< nRows; ++row) {
          cout << setw(5) << row + 1;
          printRow(row);
          cout << endl;
      }
   }

private:
   size_t maxSize;
   size_t nRows;
   size_t nColumns;
   vector<double> cont;

   static inline size_t convertToIndex(
      size_t col_i, size_t row_i, size_t numCols, size_t maxSize) {
      size_t index = row_i* numCols + col_i;
      if (index >= maxSize) { 
         throw out_of_range("calculated index past the end of the vector");
      }
      return index;
   }

   inline size_t convertToIndex(size_t col_i, size_t row_i) const {
      return convertToIndex(col_i, row_i, nColumns, maxSize);
   }
};

int main() {
   try {
      Matrix m(3,3);
      for (size_t i=0; i<3; ++i) {
          m.set(i,i, 1.0);
      }

      Matrix b(m);
      b.grow(5, 7);
      b.print();
   }
   catch (const exception& exp) {
      cerr << "EXCEPTION: " << exp.what() << endl;
   }

   return 0;
}