我不太确定这应该被视为 C++ 问题还是 wxWidgets:
我正在开发一个相当简单的 wxWidgets 界面,它允许用户绘制三角形。 因此,我有一个 Canvas (OpenGLCanvas 类)和一个 wxWindow(CMainFrame 类)问题是我希望我的 Canvas 有一个窗口实例,反之亦然。错误的根源是,当编译器到达我的 CMainFrame 实例所在的 OpenGLCanvas 部分时,它(CMainFrame)尚未定义,如果我在这里更改 #includes 的顺序,则会发生同样的事情我的代码:
OpenGLCanvas.cpp:
#include "mainframe.h"
#include "openglcanvas.h"
...
OpenGLCanvas::OpenGLCanvas(wxWindow *parent, wxWindowID id,const wxPoint& pos, const wxSize& size,long style, const wxString& name):
wxGLCanvas(parent, id, pos, size, style, name)
{
frame = (CMainFrame) parent;
}
openGLCanvas.h:
#ifndef __OPENGLCANVAS_H__
#define __OPENGLCANVAS_H__
#include "wx/wx.h"
#include <wx/glcanvas.h>
class OpenGLCanvas: public wxGLCanvas {
public:
CMainFrame* frame;
...
#endif
大型机.cpp:
#include "openglcanvas.h"
#include "mainframe.h"
...
CMainFrame::CMainFrame(wxMenuBar* menu, const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
m_menu=menu;
canvas = new OpenGLCanvas((wxWindow*)this,-1, pos , size, 0 , wxT("Canvas"));
}//constructor
...
大型机.h
#ifndef __MAINFRAME_H__
#define __MAINFRAME_H__
...
class CMainFrame: public wxFrame {
public:
CMainFrame(wxMenuBar* menu,const wxString& title, const wxPoint& pos, const wxSize& size);
我应该如何包含这些文件? (我是 C++ 的新手,但不是 C) 对于我的问题的长度,我深表歉意。任何想法都会有很大帮助。
谢谢。
最佳答案
由于您只持有一个不需要的指针,因此不建议包含整个文件。
你只需要一个forward declaration “CMainFrame 类”;
关于C++ 包括使用 wxWidgets 和 Opengl 的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13217698/