我有3个类 - GLWidget,Window和规则。 Window创建一个名为'm_glWidget'的GLWidget对象,我试图从类规则中访问它。我尝试过使用'extern'并改变一些东西,但似乎没有任何东西可以使它工作。我得到的最接近的是一个错误,上面写着“为'm_glWidget'指定的存储类”,但我不确定这意味着什么。这是我的代码:
window.h中
#include "glwidget.h"
#include "rules.h"
class GLWidget; class rules;
class Window : public QDialog, public Ui::frmMain
{
Q_OBJECT;
public:
Window(QWidget *parent = 0);
rules *gameRules;
GLWidget *m_glWidget;
Window.cpp
#include "rules.h"
#include "glwidget.h"
class rules; class GLWidget;
using namespace std;
Window::Window(QWidget *parent):QDialog(parent)
{
m_glWidget = new GLWidget;
gameRules = new rules;
rules.h
#include "windows.h"
#include "glwidget.h"
class rules{
public:
rules();
extern GLWidget *m_glWidget;
rules.cpp
#include "window.h"
#include "glwidget.h"
using namespace std;
rules::rules(){
}
void rules:: print(){
cout << m_glWidget->x << endl;
}
答案 0 :(得分:1)
这个怎么样?
#include "windows.h"
#include "glwidget.h"
class rules{
public:
rules(GLWidget *glWidget)
: m_glWidget(glWidget)
{
}
GLWidget *m_glWidget;