operator overloading question

N

none

I'm trying to overload the = operator using templates, but I have
some problems with one of the overloads, I would like to make
something like that:

intvariable = fooclass;

here's a pseudo code of what I'm doing:

template <class COMPLEXTYPE>
class foo
{
public:
COMPLEXTYPE data;

foo (void) { }

~foo (void) { }

foo<COMPLEXTYPE> operator = (foo<COMPLEXTYPE> source);
{
this->data = source.data;
return *this;
}

foo<COMPLEXTYPE> operator = (COMPLEXTYPE source);
{
this->data = source;
return *this;
}

}

// THIS IS THE SECTION THAT I'M NOT ABLE TO GET WORKING
// of unknown class
template <COMPLEXTYPE>
COMPLEXTYPE operator = (COMPLEXTYPE dest, foo<COMPLEXTYPE> source)
{
dest = (COMPLEXTYPE) source.data;
return dest;
}

main (void)
{
foo<int> FOOINT;
foo<int> FOOINT2;

int integer;

FOOTINT.data = 1;
integer = 4;

FOOINT2 = FOOINT; // works
FOOINT = integer; // works
integer = FOOINT2; // does not work
}

Anyone knows what's wrong? I know we can't overload
the basic operators but I thought I could do something
like that. Thanks in advance
 
P

Pavel Lepin

none said:
template <class COMPLEXTYPE>
class foo
{
public:
COMPLEXTYPE data;

foo (void) { }

~foo (void) { }

foo<COMPLEXTYPE> operator = (foo<COMPLEXTYPE> source);

That semicolon shouldn't be there.
{
this->data = source.data;
return *this;
}

foo<COMPLEXTYPE> operator = (COMPLEXTYPE source);

That semicolon shouldn't be there.
{
this->data = source;
return *this;
}

Not sure whether this is a good idea, but:

operator COMPLEXTYPE ()
{
return data;
}

Missing semicolon.
// THIS IS THE SECTION THAT I'M NOT ABLE TO GET WORKING
// of unknown class
template <COMPLEXTYPE>
COMPLEXTYPE operator = (COMPLEXTYPE dest, foo<COMPLEXTYPE>
source)
{
dest = (COMPLEXTYPE) source.data;
return dest;
}

main (void)

int main()
{
foo<int> FOOINT;
foo<int> FOOINT2;

int integer;

FOOTINT.data = 1;

FOOINT.data = 1;
integer = 4;

FOOINT2 = FOOINT; // works
FOOINT = integer; // works
integer = FOOINT2; // does not work
}

Anyone knows what's wrong?

Doesn't your compiler tell you what's wrong?
 
P

Pete Becker

// THIS IS THE SECTION THAT I'M NOT ABLE TO GET WORKING
// of unknown class
template <COMPLEXTYPE>
COMPLEXTYPE operator = (COMPLEXTYPE dest, foo<COMPLEXTYPE> source)

When you're lost, simplify. Try this with a class rather than a
template. Then read about user-defined assignment operators. Any basic
text will tell you what's going on.
 
D

Daniel T.

none said:
I'm trying to overload the = operator using templates, but I have
some problems with one of the overloads, I would like to make
something like that:

intvariable = fooclass;

here's a pseudo code of what I'm doing:

template <class COMPLEXTYPE>
class foo
{
public:
COMPLEXTYPE data;

foo (void) { }

~foo (void) { }

foo<COMPLEXTYPE> operator = (foo<COMPLEXTYPE> source);

unexpected ';'
{
this->data = source.data;
return *this;
}

foo<COMPLEXTYPE> operator = (COMPLEXTYPE source);

unexpected ';'
{
this->data = source;
return *this;
}

}

missing ';'
// THIS IS THE SECTION THAT I'M NOT ABLE TO GET WORKING
// of unknown class
template <COMPLEXTYPE>

missing "typename"
COMPLEXTYPE operator = (COMPLEXTYPE dest, foo<COMPLEXTYPE> source)
{
dest = (COMPLEXTYPE) source.data;
return dest;
}

operator= must be a non-static member-function

There are a couple of ways you can do this. One:

template < typename T >
T foo<T>::getData() const {
return source.data;
}

or you could:

template < typename T >
T& assign( T& dest, const foo<T>& source ) {
dest = source.data;
return dest;
}
main (void)

declaration of "main" with no type is forbidden.
{
foo<int> FOOINT;
foo<int> FOOINT2;

int integer;

FOOTINT.data = 1;

FOOTINT was not declared.
integer = 4;

FOOINT2 = FOOINT; // works
FOOINT = integer; // works
integer = FOOINT2; // does not work

Depending on what you did above, either:

integer = FOOINT2.getData();

or

assign( integer, FOOINT2 );
 
N

none

Sorry for the errors in the code, that's why
I said pseudo code, this is not the code I'm
actually using. the one I'm using do not contain
the stupid ; and main errors.

I know I could simply do this
int integer = foo.data;

but I want to do it for a complex number class and I would
really like to be able to do this:

int real = complex;
instead of
int real = complex.real;

the error I get is that the compiler do not let me overload
the = operator for
interger = foo;'

it tells me that the operator = do not belong to any class.
 
N

none

for example I tried this:

template <typename TYPE>
TYPE operator TYPE = (TYPE *type1, foo<TYPE> type2)
{
*type1 = (TYPE) type2.data;
};

and I get these errors:
user-defined conversion cannot specify a return type
incorrect user-defined conversion syntax: illegal indirections

I also tried:

template <typename TYPE>
TYPE operator TYPE = (foo<TYPE> type2)
{
return (TYPE) type2.data;
};

or

template <typename TYPE>
TYPE operator TYPE = (TYPE type1, foo<TYPE> type2)
{
type1 = (TYPE) type2.data;
return type1;
};

and

template <typename TYPE>
operator TYPE = (TYPE type1, foo<TYPE> type2)
{
type1 = (TYPE) type2.data;
};

none of them works.
 
N

none

I would also like to be able to do

foo fooobj,fooobj2;
fooobj = integer + fooobj2;

but I can't find a way to do this, the only overloads I
was able to make is:

foo fooobj,fooobj2;
fooobj = fooobk + integer;

and

foo fooobj,fooobj2;
fooobj = fooobj + fooobj2;
 
D

Daniel T.

none said:
Sorry for the errors in the code, that's why
I said pseudo code, this is not the code I'm
actually using. the one I'm using do not contain
the stupid ; and main errors.

I know I could simply do this
int integer = foo.data;

but I want to do it for a complex number class and I would
really like to be able to do this:

int real = complex;

That's not a smart thing to do. What if I want the imaginary part?

int imaginary = complex; // how can the compiler tell?
the error I get is that the compiler do not let me overload
the = operator for
interger = foo;'

it tells me that the operator = do not belong to any class.

operator= must be a non-static member-function. [full stop]
 
J

Joe Greer

I would also like to be able to do

foo fooobj,fooobj2;
fooobj = integer + fooobj2;

but I can't find a way to do this, the only overloads I
was able to make is:

foo fooobj,fooobj2;
fooobj = fooobk + integer;

and

foo fooobj,fooobj2;
fooobj = fooobj + fooobj2;

You can do that by declaring your binary operators outside your class.
In the 'a picture is worth a thousand words category', here is a small
sample program.

#include <iostream>
#include <ostream>

class foo
{
public:
foo(int a) : m_a(a) {}
foo & operator+=(foo const & a)
{
m_a += a.m_a;
return *this;
}
void print(std::eek:stream & o) const
{
o << m_a;
}
private:
int m_a;
};

std::eek:stream & operator<<(std::eek:stream & o, foo const & f)
{
f.print(o);
return o;
}

foo operator+(int a, foo const & f)
{
foo aFoo(a); // convert the int to a foo
aFoo += f;
return aFoo;
}

foo operator+(foo const & f, int a)
{
foo aFoo(a); // convert the int to a foo
aFoo += f;
return aFoo;
}

int main()
{
foo a(2);
foo b(0);

b = a + 4;
std::cout << "b = " << b << std::endl;
b = 4 + a;
std::cout << "b = " << b << std::endl;
}


Hope that helps,
joe
 
N

none

You can do that by declaring your binary operators outside your class.  
In the 'a picture is worth a thousand words category', here is a small
sample program.

#include <iostream>
#include <ostream>

class foo
{
public:
   foo(int a) : m_a(a) {}
   foo & operator+=(foo const & a)
   {
      m_a += a.m_a;
      return *this;
   }
   void print(std::eek:stream & o) const
   {
      o << m_a;
   }
private:
   int m_a;

};

std::eek:stream & operator<<(std::eek:stream & o, foo const & f)
{
   f.print(o);
   return o;

}

foo operator+(int a, foo const & f)
{
   foo aFoo(a); // convert the int to a foo
   aFoo += f;
   return aFoo;

}

foo operator+(foo const & f, int a)
{
   foo aFoo(a); // convert the int to a foo
   aFoo += f;
   return aFoo;

}

int main()
{
   foo a(2);
   foo b(0);

   b = a + 4;
   std::cout << "b = " << b << std::endl;
   b = 4 + a;
   std::cout << "b = " << b << std::endl;

}

Hope that helps,
joe

thanks a lot, yes it will help. I will try it tonight. will
it work with templates? because I think I tried something
similar to this yesterday without any success. my binary
operators are already declared outside the class.
 
E

Erik Wikström

I'm trying to overload the = operator using templates, but I have
some problems with one of the overloads, I would like to make
something like that:

intvariable = fooclass;

You can do that with a conversion operator:

#include <iostream>

class Foo
{
int m_i;

public:
Foo(int i) : m_i(i) {}
operator int() { return m_i; }
};

int main()
{
Foo f(4);
int i = f;
std::cout << i;
}
 
D

Daniel T.

Erik Wikström said:
On 2008-01-10 13:47, none wrote:

You can do that with a conversion operator:

#include <iostream>

class Foo
{
int m_i;

public:
Foo(int i) : m_i(i) {}
operator int() { return m_i; }
};

int main()
{
Foo f(4);
int i = f;
std::cout << i;
}

That's the unsafe choice. Better would be to make the constructor
explicit and provide a named conversion operator. That way you can tell
exactly what is going on at the call point and no mysterious temporaries
are created.
 
D

Daniel T.

none said:
I would also like to be able to do

foo fooobj,fooobj2;
fooobj = integer + fooobj2;

but I can't find a way to do this, the only overloads I
was able to make is:

foo fooobj,fooobj2;
fooobj = fooobk + integer;

and

foo fooobj,fooobj2;
fooobj = fooobj + fooobj2;

Why invent a Complex class where there is already a well designed one in
the language. Just use std::complex.
 
N

none

Why invent a Complex class where there is already a well designed one in
the language. Just use std::complex.- Hide quoted text -

- Show quoted text -

for fun, and because I use visual c++ and I thought that it
do not include the complex class, but maybe I'm wrong.
 
N

none

That's the unsafe choice. Better would be to make the constructor
explicit and provide a named conversion operator. That way you can tell
exactly what is going on at the call point and no mysterious temporaries
are created.- Hide quoted text -

- Show quoted text -

you have any code example, because I have no idea of how
I should do this. thanks
 
D

Daniel T.

none said:
you have any code example, because I have no idea of how
I should do this. thanks

class Foo {
int i;
public:
explicit Foo( int i ): i( i ) { }
int getI() const { return i; }
};

int main() {
Foo f( 4 );
int i = f.getI();
std::cout << i;
}
 
N

none

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;

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 :

// 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>::eek:perator =
(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>;
template Ccomplex<int>& operator + (Ccomplex<int> &dest, \
Ccomplex<int> &source);


// program.cpp

#include "complex.h"

int main(FT_INT argc, FT_CHAR* argv[])
{
KCOMPLEX complex;
KCOMPLEX complex2;

complex.real = 5;
complex.img = 2;

// WORKS
complex2 = (COMPLEX) 1 + complex;
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;
}
 
N

none

I made some modification to my code because I noticed
some errors. I have removed every & for the operator +
and added the newdest temp object because I the old code
changed the value of the dest object, and I do not want
that.

template <typename COMPLEXTYPE>
inline Ccomplex<COMPLEXTYPE> operator + (Ccomplex<COMPLEXTYPE> dest,
Ccomplex<COMPLEXTYPE> source)
{
Ccomplex<COMPLEXTYPE> newdest;

newdest.real = dest.real + source.real;
newdest.img = dest.img + source.img;

return newdest;
}
 
N

none

And I have added this assignment operator as a member
of the class:

inline Ccomplex<COMPLEXTYPE> operator = (COMPLEXTYPE source)
{
this->real = source;
this->img = 0;

return *this;
}
 
D

Daniel T.

none said:
And I have added this assignment operator as a member
of the class:

inline Ccomplex<COMPLEXTYPE> operator = (COMPLEXTYPE source)
{
this->real = source;
this->img = 0;

return *this;
}

Why are you throwing away the imaginary part? Why all the copies? Why
even write the function when the compiler supplied op= does the right
thing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top