This topic is 4079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I need some help with operator overloading. If you can give me at least some reference material, that would be great... What I want to do is define some custom structures, sort of like this;
struct My_Type
{
int one;
int two;
};

And then I want the to make the following code make sense: (this is all examples)
My_Type a, b, c;

c = a + b;
cout << c;
c = a / b;
cout << c;
c = a * b;
cout << c;

I've already read that this is possible, with operator overloading, but I don't know how to do it... In the end I want to define my own data type thanks for any kind of help. [Edited by - X-ed_out on July 25, 2007 9:45:53 AM]

##### Share on other sites
C++ FAQ Lite on operator overloading. Similarly, Google results, most of which are relevant.

##### Share on other sites

I've only seen operator's overloaded within classes, but not structures. I guess since structures have the same capabilities as classes, then you can write a operator overloader within one.

edit:

Mmm yeah the link above (same as mine) is a good read and seems to cover overloading operators well.

##### Share on other sites
thanks for the help, I'll see if I can do it with structs and if I can't I'll just use a class instead, it's the same thing...

##### Share on other sites
ok I checked out those sites and I think I'm doing everything correctly, but I can't compile this code... Can anyone tell me what I'm doing wrong?

class Rational{public:	bool Simplify();	int Top;	int Bottom;private:};Rational& operator=(const Rational& R){	Top = R.Top;	Bottom = R.Bottom;	return *this;}int& operator=(const Rational& R){	return R.Top/R.Bottom;}const Rational operator+(const Rationl& R){	Rational Res;	if(Bottom == R.Bottom)	{		Res.Top = Top + R.Top;		Res.Bottom = Bottom;	}	else	{		Res.Top = Top * R.Bottom + R.Top * Bottom;		Res.Bottom = Bottom * R.Bottom;	}	return Res;}const Rational operator-(const Rationl& R){	Rational Res;	if(Bottom == R.Bottom)	{		Res.Top = Top - R.Top;		Res.Bottom = Bottom;	}	else	{		Res.Top = Top * R.Bottom - R.Top * Bottom;		Res.Bottom = Bottom * R.Bottom;	}	return Res;}

these are the first errors it gives me:

1>error C2801: 'operator =' must be a non-static member1>error C2065: 'Top' : undeclared identifier1>error C2065: 'Bottom' : undeclared identifier1>error C2673: 'operator =' : global functions do not have 'this' pointers1>error C2556: 'int &operator =(const Rational &)' : overloaded function differs only by return type from 'Rational &operator =(const Rational &)'1>see declaration of 'operator ='

##### Share on other sites
Just a quick glance, it looks like you close off your class definition before you declare your functions. Or didn't you want them to be members of the class?

##### Share on other sites
well, maybe I could put most of them inside, but notice that one of them is to equal an int to my class so I think that one must be outside... but I might be mistaken!

(edit)
I just placed them inside, except the "rational to int" one and now it gives me less errors, so that's an improvement... but I still get these errors:

1>c:\documents and settings\propietario\os meus documentos\visual studio
2005\projects\render\matrix\header.h(53) : error C2801: 'operator =' must be a
non-static member

I just pasted the first...

int& operator=(const Rational& R){	return R.Top/R.Bottom;}

##### Share on other sites
And what does the code look like now, after you've moved them "inside?"

By the way, the operator= that returns an int should be removed. You don't want that to happen, it does not do what you think it does, and it is also the source of some compilation errors (functions are only overloaded based on their parameters, not their return type, your operator= overloads have the same parameters, so that's an error).

You rarely, if ever, want the basic type of an operator= overload to be something other than the class itself.

##### Share on other sites
oh, ok.

Then take that class I defined:

class Rational{public:	bool Simplify();	int Top;	int Bottom;	Rational& operator=(const Rational& R){		Top = R.Top;		Bottom = R.Bottom;		return *this;	}	const Rational operator+(const Rational& R){		Rational Res;		if(Bottom == R.Bottom)		{			Res.Top = Top + R.Top;			Res.Bottom = Bottom;		}		else		{			Res.Top = Top * R.Bottom + R.Top * Bottom;			Res.Bottom = Bottom * R.Bottom;		}		return Res;	}	const Rational operator-(const Rational& R){		Rational Res;		if(Bottom == R.Bottom)		{			Res.Top = Top - R.Top;			Res.Bottom = Bottom;		}		else		{			Res.Top = Top * R.Bottom - R.Top * Bottom;			Res.Bottom = Bottom * R.Bottom;		}		return Res;	}private:};

consider that this is a new way for the computer to represent numbers (as if they were fractions) and you want to print the value to the screen, so you would have to divide Top by Bottom and place that in a double or so... can't you overload the = operator to do this automatically? Because it's boring to have to write the division every time...

##### Share on other sites
Quote:
 can't you overload the = operator to do this automatically?

No. operator= is only invoked when you are assigning a value to an instance of your class. In other words, the right-hand-side of the = will always be of type Rational. That's why you want the result of operator= to be of type Rational, otherwise, very weird and nasty things will happen.

You can define an implicit conversion from Rational to int. However, this is almost always a bad idea. The best option for this behavior you want is to provide a member function called toInt() whatever. It's much clearer, and safer.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632630
• Total Posts
3007526
• ### Who's Online (See full list)

There are no registered users currently online

×