0

The error message means that your int energySystem::getEnergyLevel() should be made a const member function in order to be called with a const energySystem object. That is, you need the following prototype:

int energySystem::getEnergyLevel() const; //notice 'const' here.
1

Any "getter"-style functions should be declared as "const" functions.
i.e.:

int energySystem::getEnergyLevel() const;

Notice the "const" in the declaration...

The "this" is an "invisible" pointer that all non-static member functions have. It is a pointer to the object that called the function.

For example, if you have the call a.getEnergyLevel() in your code, "this" would be a pointer to the object that was used to call the function (the object of type energySystem identified as 'a'). It is used inside the definition/implementation of member functions to explicitly resolve the scope of identifiers that are members of the same class, such as other functions or member variables. Your energySystem::getEnergyLevel() function could be re-written as:

int energySystem::getEnergyLevel() const {
  return this->energyLevel;
}

EDIT:
Oops, Mike beat me to it...

Edited by Fbody: n/a

0

Yay, I finally got it to work :)

Thank you for all the help, especially FBody.

Edited by ben1996123: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.