Reputation: 9
This is the error message I get when I try to build:
invalid operands of types ‘double’ snd const char [3]’ to binary 'operator<<'
Obviously I'm really new to this. Any help would be appreciated.
The code reads:
#include <iostream>
using namespace std;
int main ()
{
double x = 3;
double y = 4;
cout << "(" << x = y++ << ", " << y << ")" << endl;
cout << "(" << x = ++y << ", " << y << ")" << endl;
cout << "(" << x = y-- << ", " << y << ")" << endl;
cout << "(" << x = --y << ", " << y << ")" << endl;
return 0;
}
Upvotes: 0
Views: 1790
Reputation: 254701
Assignment (=
) has a lower precedence than shift (<<
), so you need parantheses to get the meaning you expect:
cout << "(" << (x = y++) << ", " << y << ")" << endl;
^ ^
But you shouldn't be writing code with multiple side-effects like this: the evaluation order of the operands isn't specified, and this sort of thing can easily lead to undefined behaviour. Keep your code simple, doing one thing at a time.
Upvotes: 0
Reputation: 122493
=
has a lower precedence than <<
, change it to:
std::cout << "(" << (x = y++) << ", " << y << ")" << std::endl;
// ^ ^
Upvotes: 1