help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

g++ Compile Problems Using Multiple Overloaded Operators in a Matrix Cla


From: alkalineearth
Subject: g++ Compile Problems Using Multiple Overloaded Operators in a Matrix Class
Date: 14 Jan 2005 15:54:27 -0800
User-agent: G2/0.2

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 );
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]