2013-02-26 79 views
0


状态设计模式 - 编译错误

我得到三个错误,同时我尝试编译这个程序。

我期待下面的输出

OFF-ctor的
进入0/1:0已经关闭
进入0/1:1
从OFF打算在ON-ctor的析构函数-OFF
进入0/1:1
已经打开
进入0/1:0
从要去关关-ctor的 析构函数-ON
进入0/1:1
从OFF打算在ON-ctor的 析构函数-OFF
输入0/1:0
从ON变为OFF OFF-ctor的 析构函数-ON
进入0/1:0
已经关闭,进入0/1:

以下是节目

#include <iostream> 
    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 << " already ON\n"; 
     } 
     virtual void off(Machine *m) 
     { 
      cout << " already OFF\n"; 
     } 
    }; 

    void Machine::on() 
    { 
     current->on(this); 
    } 

    void Machine::off() 
    { 
     current->off(this); 
    } 

    class ON: public State 
    { 
     public: 
     ON() 
     { 
      cout << " ON-ctor "; 
     }; 
     ~ON() 
     { 
      cout << " dtor-ON\n"; 
     }; 
     void off(Machine *m); 
    }; 

    class OFF: public State 
    { 
     public: 
     OFF() 
     { 
      cout << " OFF-ctor "; 
     }; 
     ~OFF() 
     { 
      cout << " dtor-OFF\n"; 
     }; 
     void on(Machine *m) 
     { 
      cout << " going from OFF to ON"; 
      m->setCurrent(new ON()); 
      delete this; 
     } 
    }; 

    void ON::off(Machine *m) 
    { 
     cout << " going from ON to OFF"; 
     m->setCurrent(new OFF()); 
     delete this; 
    } 

    Machine::Machine() 
    { 
     current = new OFF(); 
     cout << '\n'; 
    } 

    int main() 
    { 
     void(Machine:: *ptrs[])() = 
     { 
     Machine::off, Machine::on//Error2->invalid use of non-static member function 'void Machine::off()' 
//Error3->invalid use of non-static member function 'void Machine::on()' 
     }; 
     Machine fsm; 
     int num; 
     while (1) 
     { 
     cout << "Enter 0/1: "; 
     cin >> num; 
     (fsm. *ptrs[num])(); //Error1->expected unqualified-id before '*' token 
     } 
    } 

的代码是从sourcemaking.com下拍摄状态设计模式。 我在eclipse和linux g ++中运行它。 请帮忙。

回答

1
void (Machine::*ptrs[])() = 
    { 
    &Machine::off, // note: explicit & 
    &Machine::on 
    }; 
Machine fsm; 
int num; 
while (1) 
{ 
    cout << "Enter 0/1: "; 
    cin >> num; 
    (fsm.*ptrs[num])(); // note: no space between . and * 
} 

这仍然留下了以下警告:

try.cc:19:22: warning: unused parameter 'm' [-Wunused-parameter] 
try.cc:23:22: warning: unused parameter 'm' [-Wunused-parameter] 
try.cc: In member function 'virtual void OFF::on(Machine*)': 
try.cc:68:20: warning: deleting object of polymorphic class type 'OFF' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor] 
try.cc: In member function 'virtual void ON::off(Machine*)': 
try.cc:76:14: warning: deleting object of polymorphic class type 'ON' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor] 
+0

非常感谢你.. – 2013-02-26 08:35:19

2

为了得到一个指向一个成员函数,你需要使用一个&(尽管它是可选的用于获取一个指向非成员函数):&Machine::off, &Machine::on

对于其他的,你需要认识到, .*是一个道理,所以你需要删除两个字符之间的空间:(fsm.*ptrs[num])();

+0

谢谢你很多杰瑞。 – 2013-02-26 08:35:03