Like Tree4Likes

Operator Overloading problem

This is a discussion on Operator Overloading problem within the C++ Programming forums, part of the General Programming Boards category; Ugh alright thanks, guess Ill be hitting up Google all night.....

  1. #31
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Ugh alright thanks, guess Ill be hitting up Google all night..

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by omGeeK
    Why is the compiler giving me problems on the swap saying its invalid argument?
    Uh, because you are not using your temp variable. It should have been:
    Code:
    Matrix temp(mMatrix);
    swap(temp);
    You cannot swap with mMatrix as mMatrix is a const reference, but the parameter of swap is a non-const reference.

    Quote Originally Posted by grumpy
    Because your swap is a non-static member function, and can only be used in the form "some_object.swap(some_other_object)" rather than as a naked function call.
    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);
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #33
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Quote Originally Posted by laserlight View Post
    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);
    Indeed. Place that within the 3% mentioned in my signature
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

  4. #34
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    That test for self-assignment is bad practice.
    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.

    Soma

  5. #35
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    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;
    }
    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);
    I get an error on the temp.clear() call when it uses the destructor of the temp matrix.
    Last edited by omGeeK; 04-13-2012 at 03:24 PM.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator Overloading problem
    By FMan in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2008, 12:37 PM
  2. operator overloading problem
    By salmansalman in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2008, 07:00 AM
  3. Problem with overloading an operator.
    By apacz in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2006, 10:00 AM
  4. operator overloading problem~
    By black in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2004, 09:04 AM
  5. Problem with Operator Overloading
    By bench386 in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 12:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21