Thread: Simple operator overloading issue

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Simple operator overloading issue

    Hey guys. Still working on that matrix class. I thought I'd have no problems making something as simple as scalar * matrix but it appears I'm wrong. I create an identity matrix 5x5 and simply multiply it by the scalar 2. I then assign the result to another matrix and output the new matrix. The new matrix has only zeros but it should be a diagonal matrix with only 2's... Here's the relevant code:
    Code:
    template < class T >
    Matrix< T >::Matrix< T >(const Matrix& m)
    {
    	*this = m;
    }
    
    template < class T >
    Matrix< T > Matrix< T >::IdentityMatrix(unsigned int size)
    {
    	Matrix< T > tmp(size, size);
    
    	for(int j = 0; j < tmp.Rows; j++)
    	{
    		for(int i = 0; i < tmp.Cols; i++)
    		{
    			tmp.Data[j][i] = i == j ? 1 : 0;
    		}
    	}
    
    	return tmp;
    }
    
    template < class T >
    Matrix< T >& Matrix< T >::operator = (const Matrix< T >& m)
    {
    	Rows = m.Rows;
    	Cols = m.Cols;
    
    	Data.assign(m.Data.begin(), m.Data.end());
    
    	return *this;
    }
    
    template < class T >
    Matrix< T > Matrix< T >::operator * (const T& val) const
    {
    	Matrix< T > tmp(Cols, Rows);
    
    	if(Rows > 0 && Cols > 0)
    	{
    		for(int j = 0; j < Rows; j++)
    		{
    			for(int i = 0; i < Cols; i++)
    			{
    				tmp.Data[j][i] *= val;
    			}
    		}
    	}
    
    	return tmp;
    }
    
    // this is the problematic code
    
    int main()
    {
    	Matrix<float> m = Matrix<float>::IdentityMatrix(5);
    	Matrix<float> m2 = m * 2;
    	m2.Output( );
    }
    What's really bugging me is that I know for sure that the first three are correct because I have already tested them. It leaves me with the last function which seems correct to me. Does anyone have an idea ?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I found the solution. I would create a temp variable that would be a 5x5 matrix, which is ok, but I wouldn't initialize it to *this. Assigning *this to it solved the problem. In terms of code:
    Code:
    template < class T >
    Matrix< T > Matrix< T >::operator * (const T& val) const
    {
    	Matrix< T > tmp(Cols, Rows);
    	tmp = *this;
    
    	if(Rows > 0 && Cols > 0)
    	{
    		for(int j = 0; j < Rows; j++)
    		{
    			for(int i = 0; i < Cols; i++)
    			{
    				tmp.Data[j][i] *= val;
    			}
    		}
    	}
    
    	return tmp;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with probably a very simple char sting issue
    By Prometheus in forum C++ Programming
    Replies: 14
    Last Post: 01-10-2007, 08:02 PM
  2. Simple issue but I am STUCK
    By jedispy in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2006, 02:02 AM
  3. ...deceptively simple...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-29-2002, 12:51 PM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  5. How can I issue AT commands in VC++ ?
    By Cube in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-10-2001, 07:45 AM