[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: g++ Compile Problems Using Multiple Overloaded Operators in a Matrix
From: |
Larry I Smith |
Subject: |
Re: g++ Compile Problems Using Multiple Overloaded Operators in a Matrix Class |
Date: |
Sat, 15 Jan 2005 01:09:15 GMT |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041220 |
alkalineearth@yahoo.com wrote:
Hello,
I am a TA for a computer science and engineering class... in the past I
have developed a class called 'matrix' that uses multiple overloaded
operators to manipulate matrix objects, (e.g. matA = matB * matC)
This code ran perfectly when I compiled it in the past using g++
version 2.91.57 (egcs Cygnus B20), but with version 3.3.3, it will not
compile since it cannot resolve the overloaded '=' operator,
appearantly due to the order in which the functions are being called. I
have included some much simplified source code below along with the
error...could anyone lend some insight into what changed between the
versions of g++ and how to get the code to work? Keep in mind that I
had to simplify the code greatly, and there may be logic errors, etc
from that process. I really just would like to get the thing to compile
as it did before.
Thanks,
AE
ERROR OUTPUT:
Class_Matrix_Tester.cc: In function `int main()':
Class_Matrix_Tester.cc:53: error: no match for 'operator=' in 'C =
matrix::operator+(matrix&)((&B))'
Class_Matrix_Tester.cc:42: error: candidates are: matrix&
matrix::operator=(matrix&)
SOURCE CODE:
class matrix
{
public:
// Constructor
matrix( int nr, int nc );
// Overload () for position addressing
double &operator() ( int row, int col );
// Overload '+' and '=' for ease of matrix manipulation
matrix operator + ( matrix &b );
matrix & operator = ( matrix &rhs );
private:
int num_rows, num_cols;
double *m_ptr;
};
matrix :: matrix( int nr, int nc )
{
num_rows = nr; num_cols = nc;
m_ptr = new double[nr*nc];
return;
}
double &matrix :: operator ( ) ( int row, int col )
{
return ( m_ptr[ num_cols * (row-1) + col - 1 ] );
}
matrix matrix :: operator+ ( matrix &b )
{
int nr, nc, i, j;
nr = (*this).num_rows; nc = (*this).num_cols;
matrix a( nr,nc ); matrix sum( nr,nc ); a = *this;
// Add 'a' (which has been set to *this) and 'b', return the sum.
for ( i=1 ; i <= (*this).num_rows ; i++ )
{ for ( j=1 ; j <= (*this).num_cols ; j++ )
sum(i,j) = a(i,j) + b(i,j); }
return ( sum );
}
matrix &matrix :: operator= ( matrix &rhs )
{
int i, j;
// Copy rhs values into current object.
for ( i=1 ; i <= (*this).num_rows ; i++ )
{ for (j=1;j<=(*this).num_cols;j++ ) (*this)(i,j) = rhs(i,j); }
return ( *this );
}
int main( )
{
matrix A(2,2); matrix B(2,2); matrix C(2,2);
C = (A + B); <-------- ERROR LINE
return( 0 );
}
operator+() should always be a 'friend' function.
Here's your code modified:
--------------------
#include <stdlib.h>
class matrix
{
public:
// Constructors
matrix (int nr, int nc);
matrix (const matrix& oth);
// Destructor to free m_ptr so memory doesn't leak
~matrix();
// Overload () for position addressing
double& operator () (int row, int col) const;
// Overload '+' and '=' for ease of matrix manipulation.
// operator+() must always be a 'friend' function.
friend matrix operator + (const matrix& lhs, const matrix& rhs);
matrix& operator = (const matrix& rhs);
private:
// make the default constructor private so an empty
// matrix can not be created.
matrix();
int num_rows, num_cols;
double *m_ptr;
};
// 'friend' method to add 2 matrices, producing/returning a 3rd
matrix
operator+(const matrix& rhs, const matrix& lhs)
{
// hmm, what if the row/col sizes of the matrices differ??
int nr, nc, i, j;
nr = rhs.num_rows;
nc = rhs.num_cols;
matrix sum (nr, nc);
// Add 'lhs' and 'rhs' into 'sum'.
for (i = 1; i <= nr; i++)
{
for (j = 1; j <= nc; j++)
sum (i, j) = rhs (i, j) + lhs (i, j);
}
return (sum);
}
matrix::matrix (int nr, int nc)
{
num_rows = nr;
num_cols = nc;
m_ptr = new double[nr * nc];
return;
}
matrix::matrix (const matrix& oth)
{
num_rows = oth.num_rows;
num_cols = oth.num_cols;
m_ptr = new double[num_rows * num_cols];
}
matrix::~matrix()
{
if (NULL != m_ptr)
{
delete[] m_ptr;
m_ptr= NULL;
}
}
double&
matrix::operator() (int row, int col) const
{
return (m_ptr[num_cols * (row - 1) + col - 1]);
}
matrix&
matrix::operator= (const matrix& rhs)
{
int i, j;
// hmm, what if the row/col sizes of the matrices differ??
// Copy rhs values into my array
for (i = 1; i <= num_rows; i++)
{
for (j = 1; j <= num_cols; j++)
operator()(i, j) = rhs (i, j);
}
return (*this);
}
int
main ()
{
matrix A (2, 2);
matrix B (2, 2);
matrix C (2, 2);
C = (A + B); // <--------ERROR LINE - not anymore...
return (0);
}
-------------
Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.