0
#include <iostream> using namespace std; class Actor { private: int health; int atkPower; public: Actor() { health = 100; atkPower = 10; } int getHealth() { return health; } void setHealth(int val) { health = val; } }; int main() { Actor player; Actor enemy; while (enemy.getHealth <<= 0) { } return 0; } 

The while loop is the error. How can I use the return value of the function for that loop without getting an error? Also is it possible if someone could explain to me why exactly this code isn't acceptable to the compiler? Thank you in advance.

2
  • If you're asking a question about a compile error, you should really paste that error. In this case it's easy to see the problem, but it's rude to ask everyone to guess something the compiler already told you for free. Commented Oct 19, 2019 at 15:19
  • You should review how to call functions in C++. Commented Oct 19, 2019 at 17:08

1 Answer 1

2

Turn enemy.getHealth <<= 0 into enemy.getHealth() <= 0.

<<= is an obscure C++ operator which is probably not what you are looking for. It is used as a <<= b where a = a << b. I think you mean "a less than or equal to b" which is a <= b.

Also Actor::getHealth is a function, so you need to call it as a function with no parameters with enemy.getHealth()

Sign up to request clarification or add additional context in comments.

2 Comments

<<= is a C++ operator :)
Aplogies, you're right. It means, "bitshift myself". x <<= y is equivalent to x = x << y. Certainly not what OP was trying to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.