简体   繁体   中英

C2143: syntax error : missing ';' before '*'

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

You have circular include, forward declaration MainSession type to break the current circula include issue.

In MainWindow.h

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

class MainSession;     // add forward declaration
class MainWindow : public QMainWindow
{
    //...
    MainSession *ms; //Error here.
};

I have checked your code like this:

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

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

See here in my code where MainSession is missing and I got same error in the line. Hope it will help. MainSession definition may missing because of file missing, file not included, scope problem (another namespace) etc. Please check these. namespace Ui (different) probably the problem.

Problem Is Solved by using the observer Model.

A Complete Demo Here

Add a Comment If you want the working code of the above code.

Cheers!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM