Note that there are some explanatory texts on larger screens.

plurals
  1. POCompilation error when using pointer to member function
    primarykey
    data
    text
    <p>This is an example of State machine design pattern .. i am facing some issue in it please explain and give solution for it .</p> <p>This is the code:</p> <pre><code>#include &lt;iostream&gt; using namespace std; class Machine { class State *current; public: Machine(); void setCurrent(State *s) { current = s; } void on(); void off(); }; class State { public: virtual void on(Machine *m) { cout &lt;&lt; " already ON\n"; } virtual void off(Machine *m) { cout &lt;&lt; " already OFF\n"; } }; void Machine::on() { current-&gt;on(this); } void Machine::off() { current-&gt;off(this); } class ON: public State { public: ON() { cout &lt;&lt; " ON-ctor "; }; ~ON() { cout &lt;&lt; " dtor-ON\n"; }; void off(Machine *m); }; class OFF: public State { public: OFF() { cout &lt;&lt; " OFF-ctor "; }; ~OFF() { cout &lt;&lt; " dtor-OFF\n"; }; void on(Machine *m) { cout &lt;&lt; " going from OFF to ON"; m-&gt;setCurrent(new ON()); delete this; } }; void ON::off(Machine *m) { cout &lt;&lt; " going from ON to OFF"; m-&gt;setCurrent(new OFF()); delete this; } Machine::Machine() { current = new OFF(); cout &lt;&lt; '\n'; } int main() { void(Machine:: *ptrs[])() = { Machine::off, Machine::on }; Machine fsm; int num; while (1) { cout &lt;&lt; "Enter 0/1: "; cin &gt;&gt; num; (fsm. *ptrs[num])(); } } </code></pre> <p>This is the error:</p> <blockquote> <p>prog.cpp: In function ‘int main()’:<br> prog.cpp:89: error: invalid use of non-static member function ‘void Machine::off()’<br> prog.cpp:89: error: invalid use of non-static member function ‘void Machine::on()’<br> prog.cpp:97: error: expected unqualified-id before ‘*’ token</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload