Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Problem overloading - operator


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 noatom   Members   -  Reputation: 785

Like
0Likes
Like

Posted 09 April 2013 - 04:52 AM

I want to be able to write an expression like: a+b-c + and - will be overloaded

 

Here;s my class:

 

class go{
	
public:
	int i;
	const go operator++(int){
		go before;
		before.i = this->i;
		i++;
		return before;
	}

	const go operator+(go& m){
		go x;
		x.i= this->i + m.i;
		return x;

	}

	const go operator-(go& m){
		go x;
		x.i = i - m.i;
		return x;
	}

	void print(ostream& e){
		e << this->i << endl;

	}
};

 

 

If I create 3 objects of type go,initialize all of them,and do the above expression,a+b-c,on - it will show an error like:

no operator - matches these operands.

 

What is wrong?

 

 

 

Solved: define the operator overloaded functions as global,but make them a friend to the class.That way,you can get your hands on both operands!


Edited by noatom, 09 April 2013 - 04:57 AM.


Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4261

Like
6Likes
Like

Posted 09 April 2013 - 04:56 AM

The return value of your operators does not need to be const. It doesn't harm the issue, but is unnecessary. The operator itself should be a const function. The parameter needs to be a const reference. For example

go operator + (const go& m) const
{
   // whatever
}
Similar for the other operators.

#3 Juliean   GDNet+   -  Reputation: 2694

Like
7Likes
Like

Posted 09 April 2013 - 04:57 AM

You are returning "const go" from each of your operators. The operators themself take non-const reference, so this won't work. Returning by const value is useless anyway, just return "go". Also you should/need to define the input paramter to be "const go&".

 

EDIT: Damn, to slow ;)

 

EDIT2: Out of interest, did you really understand what you've been told that last thread you asked about the "temporary const return value"-thingy? Because this is pretty much related to this question...

 

EDIT3:

Solved: define the operator overloaded functions as global,but make them a friend to the class.That way,you can get your hands on both operands!

 

Thats utterly ugly. You shouldn't do that, declare the functions properly like described by me BitMaster, don't relate to such "hack-arounds" for simple things as operator overloading.


Edited by Juliean, 09 April 2013 - 05:01 AM.


#4 BitMaster   Crossbones+   -  Reputation: 4261

Like
3Likes
Like

Posted 09 April 2013 - 05:04 AM

Solved: define the operator overloaded functions as global,but make them a friend to the class.That way,you can get your hands on both operands!

There are sometimes valid reasons for friend functions instead of member function. Not understanding essential and basic things like constness and parameter passing is not one of them though. Sorry for sounding blunt, but you really need a lot of more knowledge and experience before you should decide to do a "Solved" in obnoxious font sizes.

#5 Chad Smith   Members   -  Reputation: 1139

Like
0Likes
Like

Posted 09 April 2013 - 06:45 AM

Solved: define the operator overloaded functions as global,but make them a friend to the class.That way,you can get your hands on both operands!


Like has been said, this may have "solved" it but I would not call it proper and a hacked solution.

The proper solution for what you want was posted the first and second response. I would edit your post just so other people maybe having same problem don't think that is the proper solution.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS