Referencing the calling object within a function

I'm creating a fraction class from scratch, and, in doing so, defining operator +,-,*,/ (and += etc). I already wrote the code for + and += and they work fine. I'm now trying to write -= by multiplying the second fraction by -1 and adding them. However, it must be written as a member function and I'm getting an error when trying to reference the calling object. This is what I tried:

1
2
3
4
5
6
7
8
	void fraction::operator-=(const fraction& source)
	{
		fraction f1=source;	// Local fraction, allowing us to manipulate source

		f1.num *=-1;

		this+=f1;
	}


That gives me a compiler error, saying there is no operator that can take the fraction type (should be noted that I got that error before, and it was because tried to change the wrong item, a constant, so I'm not really sure how much sense it makes).
Last edited on
"this" is a pointer to the calling object, so on line 7 you need to dereference it.
That's what I figured, but is there any way to reference the entire calling object? It cleans up the code a fair bit if I can.
I don't know what you mean but you need to write it like so (*this)+=f1; to solve the error
Topic archived. No new replies allowed.