Accessor declared inside the class, and defined outside the class doesn’t work

  accessor, c++, class
//inside the class NutritionData, declaration
double getCaloriesPerServing() const;

//Outside the class Nutrition Data, definition
double NutritionData::getCaloriesPerServing() const {
    calPerServing = calFromCarb+calFromFat+calFromProtein;
    return calPerServing;
}

Error: says

the expression calPerServing must be a modifiable lvalue.

Instructor directions:
1.Add a const member function named getCaloriesPerServing that returns the total calories from carb, protein and fat. Again, don’t forget to mark this function as a const member function. Define this function outside of the class.

Source: Windows Questions C++

LEAVE A COMMENT