Why do i get invalid use of non-static member function error?

Aug 7, 2010 at 9:21am
I'm doing the exercised from the book Teach Yourself C++ in 21 Days and in one of the exercises I get error: invalid use of non-static member function 'void myClass::ShowMember()'

I got the code straight form the book after failing to resolve the issue in the program that I wrote, but I still get the same error. Here's the code:
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
#include <iostream>
using namespace std;

class myClass
{
	public:
		myClass();
		~myClass();
		void ShowMember();
		static int GetStatic();
	private:
		int itsMember;
		static int itsStatic;
};

myClass::myClass():
itsMember(1)
{
	itsStatic++;
}

myClass::~myClass()
{
	itsStatic--;
	cout << "In destructor, ItsStatic: " << itsStatic << endl;
}

void myClass::ShowMember()
{
	cout << "itsMember: " << itsMember << endl;
}

int myClass::itsStatic = 0;

int myClass::GetStatic()
{
	return itsStatic;
}

int main()
{

	void (myClass::*PMF) ();

	PMF=myClass::ShowMember;

	myClass obj1;
	(obj1.*PMF) ();
	cout << "Static: " << myClass::GetStatic() << endl;

return 0;

}


Does anyone have any idea why I get this error? Does it have to do with the compiler maybe? I'm using Ubuntu Linux 10.04 and GNU compiler, but I tried to run this program using Dev C++ and get the same error message.
Aug 7, 2010 at 9:32am
line 45 should be PMF=&myClass::ShowMember;
Aug 7, 2010 at 9:35am
Thanks, that worked perfectly! :)
Topic archived. No new replies allowed.