Why am I getting this error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Hi, i'm getting 

[Error] cannot call member function 'double InternetCharge::calcPlanCharge(int)' without object

for my function 'void displayBetterPlans(), and I just can't seem to figure out why.  Could anyone help me out?  

Thanks!




#include <iostream>
#include <iomanip>
#include <string>
#include "InternetCharge.h"

using namespace std;

InternetCharge::InternetCharge(int thePlan, int min, double dataUsed)
{
	plan = thePlan;
	minutes = min;
	data = dataUsed;
}

int InternetCharge::getPlan()
{
	return plan;
}

int InternetCharge::getMinutes()
{
	return minutes;
}

double InternetCharge::getData()
{
	return data;
}

double InternetCharge::getCharge()
{
	return charge;
}

double InternetCharge::calcPlanCharge(int thePlan)
{
	switch(thePlan)
	{
		case 1:
			//Code
			charge = 20.00;
			
			if ((static_cast<double>(minutes) / 60) > 100)
			{
				charge = (((static_cast<double>(minutes) / 60) - 100) * 1.00) + charge;
			}
			
			if ( data > 5)
			{
				charge = ((data - 5) * 10.00) + charge;
			}

			
			return charge;
			break;
			
		case 2:
			// code
			charge = 50.00;
						
			if ( data > 5)
			{
				charge = ((data - 5) * 10.00) + charge;
			}
			
			return charge;
			break;
			
		case 3:
			//Code
			charge = 80.00;
						
			if ((static_cast<double>(minutes) / 60) > 150)
			{
				charge = (((static_cast<double>(minutes) / 60) - 150) * 1.00) + charge;
			}
			
			return charge;
			break;
		
		case 4:
			//Code
			charge = 120.00;
			
			return charge;
			break;
	}
}

void InternetCharge::displayMonCharge()
{
	cout << "Charge for the month: $" << InternetCharge::getCharge() << endl;
}

void displayBetterPlans()
{
	cout << "Plans that would have saved the customer money:" << endl;
	
	if (InternetCharge::calcPlanCharge(1) < InternetCharge::getCharge())
	{
		cout << "Plan 1:	Charge: $" << InternetCharge::calcPlanCharge(1) << setw(12) << "Savings: $" << InternetCharge::getCharge() - InternetCharge::calcPlanCharge(1);
	}
} 


*Edited 1 time. Formatted source code to be easier to read. First time using this forum ;) *
Last edited on
How is your InternetCharge class set up?
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.
Last edited on
Here's my InternetCharge class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
		
};
@maeriden

But why would
cout << "Charge for the month: $" << InternetCharge::getCharge() << endl;
work without having to be a static member function?

Last edited on
It should not work. If it does, I guess I'll have some googling to do.
Did you try to compile to see if the compiler complains about that too?
Yeah I compiled it. The compiler complains about

1
2
3
4
5
6
7
8
9
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();.
Last edited on
Haha I feel a bit embarrased now... I omitted the class name scope resolution operator. Ooopppppsss.

1
2
3
4
5
6
7
8
9
void displayBetterPlans()
{
	cout << "Plans that would have saved the customer money:" << endl;
	
	if (InternetCharge::calcPlanCharge(1) < InternetCharge::getCharge())
	{
		cout << "Plan 1:	Charge: $" << InternetCharge::calcPlanCharge(1) << setw(12) << "Savings: $" << InternetCharge::getCharge() - InternetCharge::calcPlanCharge(1);
	}
}


Should have been.....

1
2
3
4
5
6
7
8
9
void InternetCharge::displayBetterPlans()
{
	cout << "Plans that would have saved the customer money:" << endl;
	
	if (InternetCharge::calcPlanCharge(1) < InternetCharge::getCharge())
	{
		cout << "Plan 1:	Charge: $" << InternetCharge::calcPlanCharge(1) << setw(12) << "Savings: $" << InternetCharge::getCharge() - InternetCharge::calcPlanCharge(1);
	}
}


Thanks for the help!
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.
Last edited on
Topic archived. No new replies allowed.