Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overriding the += operator

Status
Not open for further replies.

MuadDubby

Programmer
Sep 23, 1999
236
CA
I have been trying to override the += sign, and am getting into all sorts of compiling errors. Here is the syntax I am using:

struct my_strcut
{
public:
my_strcut& operator += (my_struct&);
.
.
.
private:
.
.
.
}

When I try to compile a program that includes this, I get the error "binary 'operator +=' has too few parameters". Should I be passing it an additional instance of my_struct?

The only reason I'm passing it just one parameter is becauise the use of this function is very similar to that of the '=' operator, and that one takes only one parameter.

Thanx!
 
david,

You appear to have typos

> my_strcut& operator += (my_struct&);

my_strcut is not the same as my_struct !!

Good luck
-pete
 
palbano -

I didn't copy and paste from my project, but rather retyped it all here. The typo is from the posted message, and not from my code. It should read:

struct my_struct
{
public:
my_struct& operator += (my_struct&);
.
.
.
private:
.
.
.
}

Any ideas?
 
david,

When I add the following implementation it compiles fine for me:

struct my_struct
{
public:
my_struct() : n(5){}
my_struct& operator += (my_struct&);
private:
int n;
};

my_struct& my_struct::eek:perator+=( my_struct& rhs){
n += rhs.n;
return *this;
}

Also you could improve your operator this way
const my_struct& operator+=( const my_struct& rhs);

Hope this helps
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top