简体   繁体   中英

invalid operands of types 'int' and 'const char [11]' to binary 'operator<<'

First of all, I'm just a beginner so I'm sorry if this would sound really stupid.

I'm currently doing a game in C++ where every move of a player corresponds to additional money and time deduction. Each player starts with 0 initial money and 30 minutes of time. I plan to have this on loop until both players have 0 time remaining.

This is part of my code:

if ((groupNumber1 == 1))
{
    cout<<"You now have a total of "<<"$"<< initialMoney += teensMoney <<" and have "<< initialTime -= teensTime <<" minutes remaining."<<endl;
}
else if ((groupNumber1 == 2))
{
    cout<<"You now have a total of "<<"$"<< initialMoney += familyMoney <<" and have "<<initialTime -= familyTime <<" minutes remaining."<<endl;  
}

Now when I run the program it gives me this:

[Error] invalid operands of types 'int' and 'const char [11]' to binary 'operator<<'

May I know where is the error? Thank you so so much!

Looks like a precedence problem. += will come AFTER <<

initialMoney += teensMoney --> ( initialMoney += teensMoney )

Because of operator precedence rules ( << is evaluated before += and -= ), you need to bracket your arithmetic expressions, like this:

if ((groupNumber1 == 1))
{
    cout<<"You now have a total of "<<"$"<< (initialMoney += teensMoney) <<" and have "<< (initialTime -= teensTime) <<" minutes remaining."<<endl;
}
else if ((groupNumber1 == 2))
{
    cout<<"You now have a total of "<<"$"<< (initialMoney += familyMoney) <<" and have "<<(initialTime -= familyTime) <<" minutes remaining."<<endl;  
}

Echoing @user4581301's point - are you sure you want to use += and -= here? That will permanently change the values stored in those variables after the printout, which I doubt is what you want to do. That would also not print out the correct values - do you see why?

Instead, just use good old fashioned + and - :

if (groupNumber1 == 1)
{
    cout << "You now have a total of " << "$" << initialMoney + teensMoney << " and have " << initialTime - teensTime << " minutes remaining." << endl;
}
else if (groupNumber1 == 2)
{
    cout << "You now have a total of " << "$" << initialMoney + familyMoney << " and have " << initialTime - familyTime << " minutes remaining." << endl;  
}

Simplifying the problem to reduce the noise we get

cout << a += b << endl;

Due to operator precedence , this is interpreted by the compiler as

(cout << a) += (b << endl);

and the b << endl stage makes no sense. You can fix this by forcing the order you want with brackets

cout << (a += b) << endl;

But I find modifying values inside output statements leads to programming mistakes because, even if you intend to update a with += , folks expect values to come and go from an output statement unchanged. If you really want to update the value of a , I would remove all ambiguity with

a+=b;
cout << a << endl;

If you don't mean to update a

cout << a + b << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM