Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#Actualnoatom

Posted 09 April 2013 - 04:57 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!


#1noatom

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?


PARTNERS