Re: operator overloading question
In article
<b28d82be-d722-462f-9452-a0557a08d871@d70g2000hsb.googlegroups.com>,
none <mikem891@hotmail.com> wrote:
I was able to do what I wanted, but not exactly the way I would
like it to work. I choose this method because I don't want to
make 3 copies for each operators overload (int,complex), (complex,int)
and (complex,complex). This way I only have to build
a function for each operators.
I'm able to do this:
COMPLEX complex;
COMPLEX complex2;
complex.real = 1;
complex.img = 3;
complex2 = (COMPLEX) 3 + complex;
complex = complex2 + (COMPLEX) 5;
but I would like to do:
complex2 = 3 + complex;
complex = complex2 + 5;
template < typename T >
class Complex
{
public:
Complex(): real(0), imag(0) { }
Complex( T r, T i ): real(r), imag(i) { }
T real;
T imag;
};
template < typename T >
Complex<T> operator+( T left, const Complex<T>& right )
{
return Complex<T>( left + right.real, right.imag );
}
template < typename T >
Complex<T> operator+( const Complex<T>& left, T right )
{
return Complex<T>( left.real + right, left.imag );
}
int main(int argc, char* argv[])
{
Complex<int> complex;
Complex<int> complex2;
complex.real = 5;
complex.imag = 2;
complex2 = 1 + complex;
assert( complex2.real == 6 );
assert( complex2.imag == 2 );
complex2 = complex + 1;
assert( complex2.real == 6 );
assert( complex2.imag == 2 );
}
but I get this error:
error C2784: 'class Ccomplex<COMPLEXTYPE> &__cdecl operator +(class
Ccomplex<COMPLEXTYPE> &,class Ccomplex<COMPLEXTYPE> &)' : could not
deduce template argument for 'class Ccomplex<COMPLEXTYPE> &' from
'const int'
Anyone knows how I could do what I want to do?
By the way I forgot about:
int integer = complex; // integer = complex.real
because I think Daniel T. is right and it's not a smart thing
to do and can be very confusing.
here's a sample of my code :
Unfortunately, your code is riddled with errors. Try posting your actual
code and I'll be happy to review it. In general though, you are making
this way harder on yourself than it has to be. There are all kinds of
unnecessary constructs in your code, and other stuff that is missing but
must be there.
// mycomplex.h
template <class COMPLEXTYPE> class Ccomplex;
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE>& operator + (Ccomplex<COMPLEXTYPE> &dest,
\
Ccomplex<COMPLEXTYPE>
&source);
// also tried
// inline Ccomplex<COMPLEXTYPE> operator + (Ccomplex<COMPLEXTYPE>
dest, \
// Ccomplex<COMPLEXTYPE>
source);
template <class COMPLEXTYPE>
class Ccomplex
{
public:
COMPLEXTYPE real;
COMPLEXTYPE img;
// Constructor
Ccomplex (void)
{
real = 0;
img = 0;
}
// Constructor (conversion operator)
Ccomplex (COMPLEXTYPE val)
{
real = val;
img = 0;
}
// Destructor
~Ccomplex (void) { }
inline Ccomplex<COMPLEXTYPE> operator = (Ccomplex<COMPLEXTYPE>
source);
friend inline Ccomplex<COMPLEXTYPE>& operator +
(Ccomplex<COMPLEXTYPE> &dest, \
Ccomplex<COMPLEXTYPE> &source);
// also tried
// friend inline Ccomplex<COMPLEXTYPE> operator +
(Ccomplex<COMPLEXTYPE> dest, \
//
Ccomplex<COMPLEXTYPE> source);
protected:
};
typedef Ccomplex<int> COMPLEX;
// mycomplex.cpp
#include "complex.h"
template <class COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> Ccomplex<COMPLEXTYPE>::operator =
(Ccomplex<COMPLEXTYPE> source)
{
this->real = source.real;
this->img = source.img;
return *this;
}
template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE>& operator + (Ccomplex<COMPLEXTYPE> &dest,
\
Ccomplex<COMPLEXTYPE>
&source)
{
dest.real = dest.real + source.real;
dest.img = dest.img + source.img;
return dest;
}
template class Ccomplex<int>;
Spurious ';'
template Ccomplex<int>& operator + (Ccomplex<int> &dest, \
Ccomplex<int> &source);
Function above declared but not defined. It's wholly unnecessary anyway.
// program.cpp
#include "complex.h"
int main(FT_INT argc, FT_CHAR* argv[])
FT_INT and FT_CHAR were not declared. Use 'int' and 'char' instead.
{
KCOMPLEX complex;
KCOMPLEX complex2;
KCOMPLEX was not declared. I changed them to COMPLEX.
complex.real = 5;
complex.img = 2;
// WORKS
complex2 = (COMPLEX) 1 + complex;
The above doesn't work. You are creating a temporary and because your
op+ takes its parameters by non-const reference, it can't be used.
complex = complex2 + (COMPLEX) 2;
// DO NOT WORK
// error C2784: 'class Ccomplex<COMPLEXTYPE> &__cdecl operator +
(class Ccomplex<COMPLEXTYPE> &,
// class Ccomplex<COMPLEXTYPE> &)' : could not
deduce template argument for
// 'class Ccomplex<COMPLEXTYPE> &' from 'const int'
complex2 = 1 + complex;
complex = complex2 + 2;
return 0;
}