成员函数指针数组:当控件到达成员函数的指针时获取0xcccccccc

时间:2009-07-07 12:51:31

标签: c++ c pointers

执行以下代码时出现错误访问冲突:

void NoAction()  
{
    (*m_psStateMachine[0][0])(); 
}
class CXcp
{
public: 
    CXcp(){}
    CXcp(WORD wXcpProtocol);
    ~CXcp();

private:
    void (*m_psStateMachine[10][16])(); 

public:     
    // Action Methods from State M/c Table in the RFC//     
    void IrcScr_6(){}
    void IrcStr_4(){}
    void TldIrcStr_4(){}
    void Tls_1(){}
    void Tld_1(){}
    void Tlf_0(){}
    void Tld(){} 
    void Str_4(){}
    void Str_5(){}
    void Scr_6(){}
    void Scr_8(){}
    void Tlf_2(){}
    void Tlf_3(){}
    void Tlf_3p(){}
    void Sta_2(){}
    void IrcScrSca_8(){}
    void Sca_8(){}
    void ScaTlu_9(){}
    void TldScrSca_8(){}
    void Scn_6(){}
    void Scn_7(){}
    void TldScrScn_6(){}
    void IrcScrScn_8(){}
    void Irc_7(){}
    void Scr_6x(){}
    void IrcTlu_9(){}
    void TldScr_6x(){}
    void Sta_3(){}
    void IrcScr_8(){}
    void Sta_4(){}
    void Sta_5(){}
    void Sta_6(){}
    void TldZrcSta_6(){}
    void TldScr_6(){}
    void Scj_2(){}
    void Scj_3(){}
    void Scj_4(){}
    void Scj_5(){}
    void Scj_6(){}
    void Scj_7(){}
    void Scj_8(){}
    void Scj_9(){}
    void TldIrcStr_5(){}
    void Ser_9(){}

    void NoAction() 
    {
        (*m_psStateMachine[0][0])();
    }
    void Initial()  {}
    void Starting() {}
    void Closed()   {}
    void Stopped()  {}
    void Closing()  {}
    void Stopping() {}
    void ReqSent()  {}
    void AckRecvd() {}
    void AckSent()  {}
    void Opened()   {}
};

CXcp::CXcp(WORD wXcpProtocol)
{


// Kernel of the PPP the State M/c : Initialization Here 
    void (CXcp :: *m_psStateMachine[10][16])() = 
    {
/*         State                                     
             0          1             2         3          4          5         6         7          8           9          
Events |  INITIAL    STARTING      CLOSED     STOPPED    CLOSING    STOPPING   Req-Sent  Ack-Rcvd   Ack-Sent    OPENED */

/*Up*/     &CXcp::Initial, &CXcp::IrcScr_6, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction,

/*Down*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::Initial, &CXcp::Tls_1, &CXcp::Initial, &CXcp::Starting, &CXcp::Starting, &CXcp::Starting, &CXcp::Starting, &CXcp::Tld_1,

/*Open*/   &CXcp::Tls_1, &CXcp::Starting, &CXcp::IrcScr_6, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::ReqSent, &CXcp::AckRecvd, &CXcp::AckSent, &CXcp::Opened,

/*Close*/  &CXcp::Initial, &CXcp::Tlf_0, &CXcp::Closed, &CXcp::Closed, &CXcp::Closing, &CXcp::Closing, &CXcp::IrcStr_4, &CXcp::IrcStr_4, &CXcp::IrcStr_4, &CXcp::TldIrcStr_4,

/*TO+*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::Str_4, &CXcp::Str_5, &CXcp::Scr_6, &CXcp::Scr_6, &CXcp::Scr_8, &CXcp::NoAction,

/*TO-*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::NoAction, &CXcp::Tlf_2, &CXcp::Tlf_3, &CXcp::Tlf_3p, &CXcp::Tlf_3p, &CXcp::Tlf_3p, &CXcp::NoAction,

/*RCR+*/  &CXcp::NoAction, &CXcp::NoAction, &CXcp::Sta_2, &CXcp::IrcScrSca_8, &CXcp::Closing, &CXcp::Stopping, &CXcp::Sca_8, &CXcp::ScaTlu_9, &CXcp::Sca_8, &CXcp::TldScrSca_8,

/*RCR-*/  &CXcp::NoAction, &CXcp::NoAction, &CXcp::Sta_2, &CXcp::IrcScrSca_8, &CXcp::Closing, &CXcp::Stopping, &CXcp::Scn_6, &CXcp::Scn_7, &CXcp::Scn_6, &CXcp::TldScrScn_6,

/*RCA*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::Sta_2, &CXcp::IrcScrScn_8, &CXcp::Closing, &CXcp::Stopping, &CXcp::Irc_7, &CXcp::Scr_6x, &CXcp::IrcTlu_9, &CXcp::TldScr_6x,

/*RCN*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::Sta_2, &CXcp::Sta_3, &CXcp::Closing, &CXcp::Stopping, &CXcp::IrcScr_6, &CXcp::Scr_6x, &CXcp::IrcScr_8, &CXcp::TldScr_6x,

/*RTR*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::Sta_2, &CXcp::Sta_3, &CXcp::Sta_4, &CXcp::Sta_5, &CXcp::Sta_6, &CXcp::Sta_6, &CXcp::Sta_6, &CXcp::TldZrcSta_6,

/*RTA*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::Closed, &CXcp::Stopped, &CXcp::Tlf_2, &CXcp::Tlf_3, &CXcp::ReqSent, &CXcp::ReqSent, &CXcp::AckSent, &CXcp::TldScr_6,

/*RUC*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::Scj_2, &CXcp::Scj_3, &CXcp::Scj_4, &CXcp::Scj_5, &CXcp::Scj_6, &CXcp::Scj_7, &CXcp::Scj_8, &CXcp::Scj_9,

/*RXJ+*/  &CXcp::NoAction, &CXcp::NoAction, &CXcp::Closed, &CXcp::Stopped, &CXcp::Closing, &CXcp::Stopping, &CXcp::ReqSent, &CXcp::ReqSent, &CXcp::AckSent, &CXcp::Opened,

/*RXJ-*/  &CXcp::NoAction, &CXcp::NoAction, &CXcp::Tlf_2, &CXcp::Tlf_3, &CXcp::Tlf_2, &CXcp::Tlf_3, &CXcp::Tlf_3, &CXcp::Tlf_3, &CXcp::Tlf_3, &CXcp::TldIrcStr_5,

/*RXR*/   &CXcp::NoAction, &CXcp::NoAction, &CXcp::Closed, &CXcp::Stopped, &CXcp::Closing, &CXcp::Stopping, &CXcp::ReqSent, &CXcp::AckRecvd, &CXcp::AckSent, &CXcp::Ser_9

    };
}

CXcp::~CXcp()
{

}

int main(void)
{
    CXcp oXcp(10);
    oXcp.NoAction();
    return 0;
}

答案将受到高度赞赏。

提前致谢

2 个答案:

答案 0 :(得分:3)

好吧,你没有初始化这个成员:

void (*m_psStateMachine[10][16])();

您要做的是在构造函数中创建一个本地数组,并初始化 。那只会失败。将成员更改为此

void (CXcp::*m_psStateMachine[10][16])();

在构造函数中,将本地数组复制到成员中(最好从本地数组的名称中删除m_前缀,这样就不会与实际的成员数组混淆。)

for(int i = 0; i < 10; i++)
  for(int j = 0; j < 16; j++)
     this->m_psStateMachine[i][j] = stateMachine[i][j];

或者,您可以单独分配每个元素,如果您喜欢巨大的痛苦:)然后使用另一种语法调用成员指针数组

(this->*m_psStateMachine[0][0])();

当然,某处你需要告诉它在哪个对象上调用它。

答案 1 :(得分:1)

该代码中存在许多错误:

  1. 代码试图将方法指针存储到m_psStateMachine中,它被声明为“返回void的函数指针数组的数组”:

    私人:         void(* m_psStateMachine [10] [16])();

  2. 稍后您使用方法指针创建一个本地/静态变量:

    void (CXcp :: *m_psStateMachine[10][16])() = ...
    这对您方法中可见的m_psStateMachine没有任何影响,您必须使用::m_psStateMachine转义访问权限。

  3. 通过方法指针数组调用方法时,您必须告诉编译器this使用哪个值,例如(this->*m_psStateMachine[0][0])()

  4. 我建议用

    行代替方法指针数组的声明
    class ... {
        static const void (CXcp::*m_psStateMachine[10][16])();
        ...
    };
    const void (CXcp::*CXcp::m_psStateMachine[10][16])() = { ... };