[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: g++ Compile Problems Using Multiple Overloaded Operators in a Matrix
From: |
Ulrich Eckhardt |
Subject: |
Re: g++ Compile Problems Using Multiple Overloaded Operators in a Matrix Class |
Date: |
Sat, 15 Jan 2005 09:42:12 +0100 |
User-agent: |
KNode/0.8.1 |
alkalineearth@yahoo.com wrote:
> 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.
Larry already pointed out a few things, but there are some I'd like to add:
Firstly, you need a good book on C++, as your code violates common rules of
programming in C++. "Thinking in C++" is available for download, else look
at the book reviews at http://accu.org. You might have guessed it already,
but your problems have nothing to do with GCC either, I'd suggest
comp.lang.learn.c-c++ or comp.lang.c++.moderated.
Lastly, in case you need to get work done with it, you should take a look
at boost(www.boost.org) which have a matrix class, too.
> 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;
> };
This looks like it lacks a destructor. Also, and contrary to what Larry
said, operator+ could be made a non-member function. However, it needs
access to the size of the matrix, which your class interface doesn't give.
> matrix :: matrix( int nr, int nc )
> {
> num_rows = nr; num_cols = nc;
> m_ptr = new double[nr*nc];
> return;
> }
The return is unnecessary. Also, I'd use unsigned integers and at least
assert() their values, plus initialise them instead of assigning to them.
Lastly a thing where I differ from Larry: there is no need to check a
pointer before invoking delete[] on it, as he does in the
> matrix A(2,2); matrix B(2,2); matrix C(2,2);
> C = (A + B); <-------- ERROR LINE
Now, about this error: the point is that a temporary can not be bound to a
non-constant reference. operator+ returns this temporary, but the
assignment operator wants a non-const reference. Fix just the that
assignment operator and it will work again.
Uli
--
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/