# why is this "not assignable? "

• (2 Pages)
• 1
• 2

## 22 Replies - 1288 Views - Last Post: 15 August 2012 - 12:30 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288899&amp;s=d5e44fa497920f91cbb7b0393a1f79d3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 sepp2k

• D.I.C Lover

Reputation: 1688
• Posts: 2,553
• Joined: 21-June 11

## Re: why is this "not assignable? "

Posted 15 August 2012 - 09:47 AM

Writing cout << a += b is the same as writing the following:

```cout << a;
cout += b;

```

Do you understand why cout += b can not work?

### #17 healix

Reputation: 2
• Posts: 67
• Joined: 29-May 11

## Re: why is this "not assignable? "

Posted 15 August 2012 - 10:15 AM

sepp2k, on 15 August 2012 - 09:47 AM, said:

Writing cout << a += b is the same as writing the following:

```cout << a;
cout += b;

```

Do you understand why cout += b can not work?

I understand why

```cout += b;

```

can't work. But what I'm confused about is adding an ostream to the '+=' overload function. How does that work?

```Vector2D& Vector2D::operator+=(const Vector2D& right)
{
this->x += right.x;
this->y += right.y;
return *this;
}

```

This post has been edited by healix: 15 August 2012 - 10:18 AM

### #18 sepp2k

• D.I.C Lover

Reputation: 1688
• Posts: 2,553
• Joined: 21-June 11

## Re: why is this "not assignable? "

Posted 15 August 2012 - 10:26 AM

healix, on 15 August 2012 - 07:15 PM, said:

I understand why

```cout += b;

```

can't work. But what I'm confused about is adding an ostream to the '+=' overload function. How does that work?

It doesn't. The fix here isn't to make += work with a stream as the left operand, it's to re-write the expression, so that the left operand of += is a and not cout << a.

Quote

### #19 Skydiver

• Code herder

Reputation: 1890
• Posts: 5,680
• Joined: 05-May 12

## Re: why is this "not assignable? "

Posted 15 August 2012 - 10:30 AM

healix, on 15 August 2012 - 10:15 AM, said:

sepp2k, on 15 August 2012 - 09:47 AM, said:

Writing cout << a += b is the same as writing the following:

```cout << a;
cout += b;

```

Do you understand why cout += b can not work?

I understand why

```cout += b;

```

can't work. But what I'm confused about is adding an ostream to the '+=' overload function. How does that work?

That is how the C++ compiler is parsing the line statement: cout << a += b
This is due to the operator precedence in C++: http://en.cppreferen...ator_precedence

healix, on 15 August 2012 - 10:15 AM, said:

```Vector2D& Vector2D::operator+=(const Vector2D& right)
{
this->x += right.x;
this->y += right.y;
return *this;
}

```

What is your question about that example? It's showing how an overload of the += operator is being implemented.

### #20 healix

Reputation: 2
• Posts: 67
• Joined: 29-May 11

## Re: why is this "not assignable? "

Posted 15 August 2012 - 10:46 AM

ok I think I see what's going on. Not sure we were on the same page.

In main I can't write
```cout << a+=b;

```

but I can write
``` a+=b;
```

which works.

so then how would I make it so that it can be written as
``` cout << a+=b;
```

### #21 Skydiver

• Code herder

Reputation: 1890
• Posts: 5,680
• Joined: 05-May 12

## Re: why is this "not assignable? "

Posted 15 August 2012 - 11:13 AM

Although parenthesis will work, just write it out as two lines.
```a += b;
cout << a;

```

### #22 healix

Reputation: 2
• Posts: 67
• Joined: 29-May 11

## Re: why is this "not assignable? "

Posted 15 August 2012 - 11:35 AM

ok, thanks everyone. really appreciated

### #23 blackcompe

• D.I.C Lover

Reputation: 1009
• Posts: 2,185
• Joined: 05-May 05

## Re: why is this "not assignable? "

Posted 15 August 2012 - 12:30 PM

Good catch, sepp2k. I totally missed that.