首页 > 解决方案 > oFx:使用 mouseEvent 初始化类实例

问题描述

如果我运行此代码,它会给我一个Thread 1: EXC_BAD_ACCESS.

topNav.h文件中:

 vector<indicatorButton> indicators;

并在topNav.cpp文件中

void topNav::setup(ofVec2f p, int n , int *appState) {

// ... other vars here
    for(int i = 1 ; i <= quantityOfPages ; i++){
        indicators[i-1].setup(i, ofVec2f(padding.x + spacing * i, padding.y ), appState );
    }



}

在 indicatorButton.cpp 文件中:

indicatorButton::indicatorButton() {
    bRegisteredEvents = false;
}

void indicatorButton::setup(int i, ofVec2f p, int *appState) {

    // this will enable our circle class to listen to the mouse events.
    if(!bRegisteredEvents) {
        ofRegisterMouseEvents(this);
        bRegisteredEvents = true;
    }

   // other variables...blah blah. 


}

如果我删除ofRegisterMouseEvents()它运行良好。除了我没有注册鼠标事件:/

我在这里做错了什么?

标签: c++exc-bad-accessopenframeworks

解决方案


指点!需要改变这个:

 vector<indicatorButton> indicators;

对此

 vector<indicatorButton *> indicators;

推荐阅读