I'm trying to build a simple matrix algebra application for University. When trying to add data from input files I use the following Method:
Matrix createMatrix(string filename, int rowRestriction, int colRestriction)
{
try{
ifstream file;
string line = "";
vector<string> curLine;
int cols = -1, rows = 0;
vector<vector<double>> values;
file.open(filename);
if(!file.is_open())
{
cout << "No file could be loaded, please check whether the input file is placed inside the working directory.\n";
throw 1;
}
while(getline(file,line))
{
rows+=1;
curLine = split(line);
if(cols == -1)
{
cols = curLine.size();
cout << "Matrix appears to have " << cols << " Columns.\n";
if(colRestriction != NO_RESTRICTION && cols != colRestriction)
{
cout << "The Matrix you provided does not fulfill the column restriction of " << colRestriction << " Columns, please check your input file.\n";
throw 2;
}
}
else if(cols != curLine.size())
{
cout << "Invalid Matrix supplied. Varying amount of columns. Please check input file " << filename << ".\n";
throw 3;
}
cout << "Saving Row "<<rows<<"\n";
values.resize(rows);
values[rows-1].resize(cols);
for(int i = 0; i < curLine.size(); i++)
{
if(isValidNumber(curLine[i]))
try
{
values[rows-1][i] = atof(curLine[i].c_str());
}
catch(int e)
{
cout << "Exception No. " << e << " has occurred. Presumably your input file does not contain valid floating point numbers.\n";
throw 4;
}
else
{
cout << "Your file contains invalid characters, please check your input file \"" << filename << "\".\n";
throw 5;
}
}
}
if(rowRestriction != NO_RESTRICTION && rowRestriction != rows)
{
cout << "The Matrix you provided does not fulfill the row restriction of " << rowRestriction << " Rows, please check your input file.\n";
throw 6;
}
cout << "Matrix Data has been read successfully, your matrix has " << rows << " Rows and " << cols << " Columns. It is " << ((rows==cols)?"":"not ") << "quadratic.\n";
Matrix m = Matrix(rows, cols, values);
m.setValidity(true);
return m;
}
catch(int e)
{
cout << "Exception No. " << e << "occurred.\n";
}
}
Here is the Constructor for 'Matrix':
Matrix::Matrix(int rows, int cols, vector<vector<double>> data)
{
this->rows = rows;
this->cols = cols;
this->data = data;
}
And here's the header file:
#pragma once
#include <vector>
using std::vector;
class Matrix
{
public:
Matrix(int rows, int cols, vector<vector<double>> data);
~Matrix(void);
int getCols();
int getRows();
private:
int rows, cols;
vector<vector<double>> data;
};
I receive the following Error Message - it only appears when the line Matrix m = Matrix(rows, cols, values);
is added (see above).
---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!
Program: ...al studio 2012\Projects\Matrixalgebra\Debug\Matrixalgebra.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1322
Expression: _CrtIsValidHeapPointer(pUserData)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
I just know that it's some rookie mistake, but I've been trying around for quite a while now without success. The algorithm itself works just fine up until the last few lines.
EDIT: Changed OP to reflect new problem
EDIT2: This new error throws because of my deconstructor, see below
Matrix::~Matrix(void)
{
delete &data;
}
Why is this the case - I'd really appreciate an explanation for this, or some learning material on the matter.