To store the functions of a class in an array the following link http://sourcemaking.com/design_patterns/state/cpp/1 contains the code like below (Machine is the class name).
void(Machine:: *ptrs[])() =
{
Machine::off, Machine::on
};
The example in that link does not compile with the g++ compiler throwing error as below
$ g++ state.cpp
state.cpp: In function ‘int main()’:
state.cpp:89:18: error: invalid use of non-static member function ‘void Machine::off()’
state.cpp:89:32: error: invalid use of non-static member function ‘void Machine::on()’
state.cpp:97:15: error: expected unqualified-id before ‘*’ token
I am using g++ version 4.5.2
$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Can an array be defined like this, i am not able to find an array defination like this any where else. If the example is correct why does not it compile.
If you define a typedef for your member functions you will simplify your code greatly.
The being said your error is due to missing “&” before the function name.
If you don’t want to use typedef the correct way is something like