I am programming this in visual studio proff. 2015 and I seem to be running into a problem. In my program, the Matrix class used to be made of a large 2D array, I wanted the size of the matrix to be chosen so I decided to switch over to a dynamically allocated array of arrays. This worked through my entire program except when calling the overloaded * operator, in Matrix.cpp, I get the error
"Run-Time Check Failure #3 - T".
Any assistance would be greatly appreciated!
Note: This is my first time dynamically allocating an pointer array of pointer arrays.
#ifndef MATRIX_H
#define MATRIX_H
#include<iostream>
#include<string>
#include<iomanip>
usingnamespace std;
constint MAXROW = 100;
constint MAXCOL = 100;
class Matrix
{
private:
string name;
int rowSize;
int colSize;
double **matrix;
public:
Matrix(); //Defult constructor
~Matrix();
Matrix(const Matrix& original); //copy constructor
void setRowSize(constint i); //sets the number of rows of the matrix
void setColSize(constint j); //sets the number of columns of the matrix
void setName(const string name1); //sets the name of the matrix
void inputValues(); //This function askes the user to input values of the matrix
void print(); //prints the matrix
Matrix& operator=(const Matrix& original);
Matrix& operator*(const Matrix& original);
};
#endif
Matrix* temp; // temp points to some arbitrary point in memory.
temp->name = (name + "*" + original.name); // Some arbitrary point in memory is treated as if it is a valid Matrix object.
Thank you for the suggestion however I originally did not have temp as a pointer at first, but I then switched it to a pointer to try to figure out the problem and the problem persists.
Then supplying the actual code that is causing the problem is surely recommended.
Edit: Looking over your code, there are quite a few potential problems. For instance: Matrix m; m.print(); results in undefined behavior.
All copies are shallow. All memory allocated is leaked.
I am using the pointers. I said I switched to then to see if it would help. I'm lazy and have no need to change it back. I'll try your suggestion in the morning. Thank you!
At the very least fix the temp problem and supply the modified code that was causing you problems before you added another one. tempshould not be a pointer and certainly not a pointer that is not initialized to anything before it's first use (and it's quite likely you ignored a compiler warning telling you this very thing.)
Returning by value means you bring the copy constructor into play... and your copy constructor has a serious problem. Perhaps you can look at it and tell me what matrix points to when it is first assigned to dereferenced?
[edit: assigned to => dereferenced. matrix is never assigned to in the copy constructor.]