Ugh alright thanks, guess Ill be hitting up Google all night..
Printable View
Ugh alright thanks, guess Ill be hitting up Google all night..
Uh, because you are not using your temp variable. It should have been:Quote:
Originally Posted by omGeeK
You cannot swap with mMatrix as mMatrix is a const reference, but the parameter of swap is a non-const reference.Code:Matrix temp(mMatrix);
swap(temp);
No, that is not the problem because in the context of a non-static member function that is a valid non-static member function call equivalent to this->swap(mMatrix);Quote:
Originally Posted by grumpy
Requiring the test for it to work is bad practice; having the test as a "short circuit" path to prevent a needless copy isn't bad practice just completely unnecessary.Quote:
That test for self-assignment is bad practice.
Soma
Alright I'm back with some heap errors. I have debugged the crap out of it and I don't understand the problem. At random points when my destructor gets called I will get heap errors even though I know there is a valid address their that was allocated by new[]..
Well heres my class code
Code:#include "Matrix.h"
//default constructor
Matrix::Matrix()
{
_name = "";
_row = 0;
_col = 0;
_mat = NULL;
}
//copy constructor call
Matrix::Matrix(const Matrix &m)
{
int i,j;
_row = m._row;
_col = m._col;
_name = m._name;
errorInput = m.errorInput;
_mat = new double *[_row];
for(i=0;i<_row;i++)
{
_mat[i] = new double [_col];
for(j=0;j<_col;j++)
{
_mat[i][j] = m._mat[i][j];
}
}
}
//destructor
Matrix::~Matrix()
{
if(_mat)
{
for(int i = 0; i < _row; i++)
{
cout << _mat[i] << "\n";
delete[] _mat[i];
}
delete[] _mat;
_mat = NULL;
}
}
//swap for the copy
void Matrix::swap(Matrix& m)
{
using std::swap;
swap(_name, m._name);
swap(_row, m._row);
swap(_col, m._col);
swap(errorInput, m.errorInput);
swap(_mat,m._mat);
}
//returns the name of a matrix
string Matrix::getName(void)
{
return _name;
}
//returns the number of rows
int Matrix::getRow(void)
{
return _row;
}
//returns the number of columns
int Matrix::getCol(void)
{
return _col;
}
//clear the matrix
void Matrix::clear(void)
{
Matrix temp;
swap(temp);
}
//this sets the name of the string passed to matrixs name
void Matrix::setName(string name)
{
_name.clear();
_name = name;
}
//this displays the matrix to cout
void Matrix::dispMatrix(void)
{
int i,j;
cout << _name << " =" << endl;
for(i=0;i<_row;i++)
{
for(j=0;j<_col;j++)
{
cout << "\t" << _mat[i][j];
}
cout << endl;
}
cout << endl;
}
//transpose a matrix
Matrix Matrix::transpose(void)
{
Matrix temp(*this);
Matrix temp1;
temp1._row = temp._col;
temp1._col = temp._row;
temp1._mat = new double *[temp1._row];
for(int i = 0; i < temp1._row; i++)
{
temp1._mat[i] = new double [temp1._col];
for(int j = 0; j < temp1._col; j++)
{
temp1._mat[i][j] = temp._mat[j][i];
}
}
return temp1;
}
//sets the data of a matrix given a matrix operator overloading
Matrix & Matrix::operator = (const Matrix& mMatrix)
{
if (this != &mMatrix) // protect against invalid self-assignment
{
Matrix temp(mMatrix);
swap(temp);
}
// by convention, always return *this
return *this;
}
std::istream& operator>>(std::istream &is, Matrix & mMatrix)
{
string tempString,tempString1;
istringstream tempNumString;
deque<string> sList;
int row = 0;
int col = 0;
int loopGo = 1;
int foundEnd = 0;
int lastCol = 0;
double tempDBL;
std::ios_base::fmtflags temp=is.flags();
cin >> tempString;
while(loopGo)
{
if(cin.peek() == '\n')
{
loopGo = 0;
}
if(tempString.find('[') != string::npos)
{
if(tempString.size() > 1)
{
tempString = tempString.substr(tempString.find('[') + 1,tempString.size());
col ++;
}
}
else if(tempString.find(']') != string::npos)
{
foundEnd = 1;
if(tempString.size() > 1)
{
tempString = tempString.substr(0,tempString.find(']'));
col ++;
}
row++;
}
else if(tempString.find(';') != string::npos)
{
if(tempString.size() > 1)
{
if(tempString.front() == ';')
{
tempString = tempString.substr(1, tempString.size());
}
else if(tempString.back() == ';')
{
tempString = tempString.substr(0, tempString.size()-1);
col++;
}
else
{
tempString1 = tempString.substr(0,tempString.find(';') - 1);
col++;
sList.push_front(tempString1);
tempString1 = tempString.substr(tempString.find(';') + 1,tempString.size());
}
}
if(lastCol == 0)
{
lastCol = col;
row++;
col = 0;
}
else if(lastCol == col)
{
row ++;
col = 0;
}
else
{
mMatrix.errorInput = 2;
return is;
}
}
else
{
col ++;
}
if(tempString != "[" && tempString != ";" && tempString != "]")
{
for(string::iterator sIT = tempString.begin(); sIT != tempString.end(); ++sIT)
{
if(*sIT < 48 || *sIT > 57)
{
mMatrix.errorInput = 3;
return is;
}
}
sList.push_back(tempString);
}
if(loopGo)
cin >> tempString;
}
if(foundEnd != 1)
{
mMatrix.errorInput = 4;
return is;
}
mMatrix.errorInput = 1;
mMatrix._row = row;
mMatrix._col = col;
mMatrix._mat = new double *[row-1];
for(int i=0;i<row;i++)
{
mMatrix._mat[i] = new double [col-1];
for(int j=0;j<col;j++)
{
tempNumString.str(sList.front());
tempNumString >> tempDBL;
sList.pop_front();
tempNumString.clear();
mMatrix._mat[i][j] = tempDBL;
}
}
is.flags(temp);
return is;
}
I get an error on the temp.clear() call when it uses the destructor of the temp matrix.Code:vector<Matrix>::iterator mIT = mList.begin();
for(int l = 0; l < pos ;l ++)
{
++mIT;
}
mList.erase(mIT);
temp.clear();
cin >> temp;
temp.setName(cmd);
addMatrix(mList,temp);