calcPlanCharge() is probably what is called a nonstatic member function. The form you used to call it is the one used for static member functions.
Nonstatic member functions require you to supply them with an instance of the class.
This can be done in several ways.
1 2 3 4 5 6 7 8 9 10
// Create an instance of the class, also called `object`
InternetCharge ic;
// Call function InternetCharge::calcPlanCharge() through the object.
// This automatically supplies the function with the object
ic.calcPlanCharge(1);
// The dot form is a cleaner way to do this
// That is, call function `calcPlanCharge` of the class `InternetCharge` and supply `ic` to it
InternetCharge::calcPlanCharge(ic, 1);
Static member functions are basically global functions that must be prefixed with the class name. They don't need an object, but they can't use variables and call functions that are nonstatic.
class InternetCharge
{
public:
// Member Functions
InternetCharge(int thePlan, int min, double dataUsed);
// Getters
int getPlan();
int getMinutes();
double getData();
double getCharge();
// Extra
double calcPlanCharge(int thePlan);
void displayMonCharge();
void displayBetterPlans();
private:
// Data Members
int plan;
int minutes;
double data;
double charge;
};
void displayBetterPlans()
{
cout << "Plans that would have saved the customer money:" << endl;
cout << "Charge for the month: $" << InternetCharge::getCharge() << endl;
}
But not
1 2 3 4
void InternetCharge::displayMonCharge()
{
cout << "Charge for the month: $" << InternetCharge::getCharge() << endl;
}
The error for displayBetterPlans() is...
[Error] cannot call member function 'double InternetCharge::getCharge()' without object
It's possible that it stops compiling when it gets to that error, so it never sees the following errors. I can't really say,
I failed to mention one important thing:
Nonstatic functions are always guaranteed to have an object (not giving it to them results in your error). This means that if you call member function func2() from inside member function func1(), the object can and will be automatically passed to func2().
So for example to call getCharge() from inside displayMonCharge(), you just need to write getCharge();.
Damn, I got it all wrong...
I thought you couldn't call nonstatic functions through the scope resolution operator, but the problem was just failing to use the right namespace.
Oh well, I learned something new, and I hope the explanation was useful to you.