5

Following bit of code is throwing error. I have no idea why. Can anyone shed some light? All codes are on different files.

#ifndef MAINSESSION_H
#define MAINSESSION_H
#include "sessionsuper.h"
#include "mainwindow.h"
class MainSession : public SessionSuper
{
public:
    MainSession();
private:

};

#include "mainsession.h"

MainSession::MainSession()
{

}

#endif // MAINSESSION_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "mainsession.h"
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    MainSession *ms;   //Error here
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //ms=new MainSession(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
#ifndef SESSIONSUPER_H
#define SESSIONSUPER_H

class SessionSuper
{
public:
    SessionSuper();
};

#endif // SESSIONSUPER_H
#include "sessionsuper.h"

SessionSuper::SessionSuper()
{
}

Error:

d:\qtsrc\untitled4\mainwindow.h:20: error: C2143: syntax error : missing ';' before '*'

d:\qtsrc\untitled4\mainwindow.h:20: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int d:\qtsrc\untitled4\mainwindow.h:20: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Am using Qt+msvc10.0 compiler.

Update:-

#ifndef MAINSESSION_H
#define MAINSESSION_H
#include "sessionsuper.h"
#include "mainwindow.h"
class MainSession : public SessionSuper
{
public:
    MainSession(MainWindow*);
private:
MainWindow *mw;
};

#endif // MAINSESSION_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "mainsession.h"
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    MainSession *ms;
};

#endif // MAINWINDOW_H
#ifndef SESSIONSUPER_H
#define SESSIONSUPER_H

class SessionSuper
{
public:
    SessionSuper();
};

#endif // SESSIONSUPER_H
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
#include "mainsession.h"

MainSession::MainSession(MainWindow mss)
{
  mw=mss;

}
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //ms=new MainSession(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
#include "sessionsuper.h"

SessionSuper::SessionSuper()
{
}

Errors:- a lot more but of same type

4

3 回答 3

5

您有循环包含,前向声明 MainSession 类型以打破当前循环包含问题。

在 MainWindow.h

//#include "mainsession.h" comment out this line

class MainSession;     // add forward declaration
class MainWindow : public QMainWindow
{
    //...
    MainSession *ms; //Error here.
};
于 2013-07-30T10:59:41.880 回答
0

我已经像这样检查了您的代码:

class MainWindow
{
public:
    explicit MainWindow();
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    MainSession *ms;   //My error also here <- see this
};

在我的代码中看到这里MainSession缺少的地方,我在该行中遇到了同样的错误。希望它会有所帮助。MainSession由于文件丢失、文件不包括、范围问题(另一个命名空间)等,定义可能会丢失。请检查这些。namespace Ui(不同)可能是问题所在。

于 2013-07-30T11:22:11.800 回答
0

问题通过使用观察者模型解决。

一个完整的演示在这里

添加评论如果您想要上述代码的工作代码。

干杯!!!

于 2013-07-30T12:57:08.217 回答