Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Irrlicht-单独文件中的EventReceiver类_C++_Visual Studio 2010_Irrlicht - Fatal编程技术网

C++ Irrlicht-单独文件中的EventReceiver类

C++ Irrlicht-单独文件中的EventReceiver类,c++,visual-studio-2010,irrlicht,C++,Visual Studio 2010,Irrlicht,我用MSVC++10开发了一个应用程序。这是一个Irrlicht项目。该应用程序绘制了两个简单的按钮。在我阅读的教程中,它被一个类MyEventReceiver使用。该类与主类位于同一文件中。我打算把班移到我的班上去。我已经这样做了,但我收到了“访问冲突错误”。我错过什么了吗?(我的意思是初始化一些东西)。代码如下: main.cpp #include <irrlicht.h> #include <driverChoice.h> #in

我用MSVC++10开发了一个应用程序。这是一个Irrlicht项目。该应用程序绘制了两个简单的按钮。在我阅读的教程中,它被一个类MyEventReceiver使用。该类与主类位于同一文件中。我打算把班移到我的班上去。我已经这样做了,但我收到了“访问冲突错误”。我错过什么了吗?(我的意思是初始化一些东西)。代码如下:

main.cpp

      #include <irrlicht.h>
      #include <driverChoice.h>
      #include "CMyEventReceiver.h"
      using namespace irr;
      using namespace core;
      using namespace video;
      using namespace gui;

    int main()
    {
        IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<u32>(640, 480), 16, false, false, false, 0);
        if (!device)
            return 1;
        device->setWindowCaption(L"Irrlicht Test");
        IVideoDriver *driver = device->getVideoDriver();
        IGUIEnvironment *guienv = device->getGUIEnvironment();
        IGUIFont *font = guienv->getFont("../debug/lucida.xml");
        guienv->addButton(rect<s32>(250, 20, 250 + 120, 50), 0, GUI_ID_QUIT_BUTTON, L"Exit", L"Exits Program");
        guienv->addButton(rect<s32>(400, 20, 400 + 120, 50), 0, GUI_ID_NEW_WINDOW_BUTTON, L"New Window", L"Launches a new Window");
        SAppContext context;
        context.device = device;
        CMyEventReceiver receiver(context);
        device->setEventReceiver(&receiver);
        while(device->run())
        {
            driver->beginScene(true, true, SColor(255, 128, 192, 255));
            guienv->drawAll();
            driver->endScene();
        }
        device->drop();
        return 0;
    }
#包括
#包括
#包括“CMyEventReceiver.h”
使用内部收益率;
使用名称空间核心;
使用名称空间视频;
使用名称空间gui;
int main()
{
IrrlichtDevice*device=createDevice(EDT_OPENGL,维度2D(640480),16,假,假,假,0);
如果(!设备)
返回1;
设备->设置窗口标题(L“Irrlicht测试”);
IVideoDriver*驱动程序=设备->获取视频驱动程序();
IGUIEnvironment*guienv=device->getGUIEnvironment();
IGUIFont*font=guienv->getFont(../debug/lucida.xml);
guienv->addButton(rect(250,20,250+120,50),0,GUI\u ID\u退出按钮,L“退出”,L“退出程序”);
guienv->addButton(rect(400,20,400+120,50),0,GUI_ID_NEW_WINDOW_按钮,L“NEW WINDOW”,L“启动新窗口”);
语境;
context.device=设备;
CMyEventReceiver接收器(上下文);
设备->设置事件接收器(&R);
同时(设备->运行()
{
驱动程序->开始场景(true,true,SColor(255,128,192,255));
guienv->drawAll();
驱动程序->结束场景();
}
设备->下降();
返回0;
}
CMyEventReceiver.h

    #pragma comment(lib, "Irrlicht.lib")

    #include <irrlicht.h>
    #include <driverChoice.h>

    using namespace irr;
    using namespace gui;
    using namespace core;

    struct SAppContext
    {
        IrrlichtDevice *device;
    };

    enum
    {
        GUI_ID_QUIT_BUTTON = 101,
        GUI_ID_NEW_WINDOW_BUTTON
    };

    class CMyEventReceiver : public IEventReceiver
    {
    public:
        CMyEventReceiver(SAppContext &context);
        virtual bool OnEvent(const SEvent &e);
    private:
        SAppContext *sac;
    };
#pragma注释(lib,“Irrlicht.lib”)
#包括
#包括
使用内部收益率;
使用名称空间gui;
使用名称空间核心;
结构上下文
{
irrlicht装置*装置;
};
枚举
{
GUI\u ID\u退出\u按钮=101,
GUI\u ID\u新建\u窗口\u按钮
};
CMYEEventreceiver类:公共IEventReceiver
{
公众:
CMyEventReceiver(SAppContext和context);
虚拟bool-OnEvent(const-SEvent&e);
私人:
SAPP*sac;
};
CMyEventReceiver.cpp

    #include "CMyEventReceiver.h"

    CMyEventReceiver::CMyEventReceiver(SAppContext &context) {}

    bool CMyEventReceiver::OnEvent(const SEvent &e)
    {

        if (e.EventType == EET_GUI_EVENT)
        {
            s32 id = e.GUIEvent.Caller->getID(); 
            IGUIEnvironment* guienv = sac->device->getGUIEnvironment();
            if (e.GUIEvent.EventType == EGET_BUTTON_CLICKED)
            {
                if (id == GUI_ID_QUIT_BUTTON)
                {
                    sac->device->closeDevice();
                    return true;
                }
                if (id == GUI_ID_NEW_WINDOW_BUTTON)
                {
                    IGUIWindow* window = guienv->addWindow(rect<s32>(100, 100, 300, 200),false, L"New window");
                    return true;
                }
            }
        }
        return false;
    }
#包括“CMyEventReceiver.h”
CMyEventReceiver::CMyEventReceiver(SAppContext和context){}
布尔CMyEventReceiver::OneEvent(const SEvent&e)
{
if(e.EventType==EET\u GUI\u事件)
{
s32 id=e.GUIEvent.Caller->getID();
IGUIEnvironment*guienv=sac->device->getGUIEnvironment();
如果(e.GUIEvent.EventType==EGET_按钮已单击)
{
if(id==GUI\u id\u退出\u按钮)
{
sac->device->closeDevice();
返回true;
}
if(id==GUI\u id\u新建窗口\u按钮)
{
IGUIWindow*window=guienv->addWindow(rect(100100300200),false,L“新窗口”);
返回true;
}
}
}
返回false;
}
如果我把代码放在文件中(稍加修改),它就可以工作了。我倾向于把它作为一个单独的文件,它更优雅,更有用

谢谢你的耐心


Ee Bb

看起来您未能在
CMyEventReciever
构造函数中分配
sac
成员变量

CMyEventReceiver receiver(context); 
但是,您的构造函数是:

CMyEventReceiver::CMyEventReceiver(SAppContext &context) {}
基本上是一个“不做任何事情”的构造函数

解决方案应该是:

CMyEventReceiver::CMyEventReceiver(SAppContext &context) : sac(&context) {}
但是,
sac
依赖于对象实际使用时处于“活动”状态的指针。所以IMO看起来是一个有缺陷的设计,但是对于简单的main()程序,应该是可以的


现在,如果程序更复杂,那么您应该使用
std::shared\u ptr
进行调查,这样指针只会在程序死机时死机。

请提供有关错误的更多详细信息。它在哪一部分说“访问冲突错误”?@Arpit在CMyEventReceiver.cpp中,就在IGUIEnvironment*guienv=sac->device->getGUIEnvironment()之前;我以以下方式更改了CMyEventReceiver.cpp:CMyEventReceiver::CMyEventReceiver(SAppContext&context){}:sac(&context){}不幸的是,我收到了一个错误。我想在CMyEventReceiver.h中也可以看到这个声明,但我不知道怎么做。