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.

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

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;

Oops, Mike beat me to it...

Yay, I finally got it to work :)

Thank you for all the help, especially FBody.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.