当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Inheritance Error

问题描述:

I made the following program, but now i am getting an error. I searched for error almost 3 hours but worthless. I want to discuss the problem with you people.

I am getting the error: expected class-name before '{' token

#ifndef ASKDIALOG_H

#define ASKDIALOG_H

#include <QDialog>

#include "umerwindow.h"

namespace Ui {

class AskDialog;

}

class AskDialog : public QDialog, public UmerWindow // HERE IS THE ERROR

{

Q_OBJECT

public:

explicit AskDialog(QWidget *parent = 0);

void showdialog();

~AskDialog();

private slots:

void on_dokbutton_clicked();

private:

Ui::AskDialog *ui;

};

#endif // ASKDIALOG_H

ASK DIALOG .CPP FILE

#include "askdialog.h"

#include "ui_askdialog.h"

AskDialog::AskDialog(QWidget *parent) :

QDialog(parent),

ui(new Ui::AskDialog)

{

ui->setupUi(this);

}

AskDialog::~AskDialog()

{

delete ui;

}

void AskDialog::on_dokbutton_clicked()

{

QString data=ui->dline->text();

showlist(data);

}

ASK DIALOG .CPP FILE

#include "askdialog.h"

#include "ui_askdialog.h"

AskDialog::AskDialog(QWidget *parent) :

QDialog(parent),

ui(new Ui::AskDialog)

{

ui->setupUi(this);

}

AskDialog::~AskDialog()

{

delete ui;

}

void AskDialog::on_dokbutton_clicked()

{

QString data=ui->dline->text();

showlist(data);

}

Ok so this the header file of UmerWindow

#ifndef UMERWINDOW_H

#define UMERWINDOW_H

#include <QMainWindow>

#include <QListWidgetItem>

#include "askdialog.h"

namespace Ui {

class UmerWindow;

}

class UmerWindow : public QMainWindow

{

Q_OBJECT

public:

explicit UmerWindow(QWidget *parent = 0);

~UmerWindow();

protected:

void showlist(QString &);

private slots:

void on_actionAdd_Item_triggered();

private:

Ui::UmerWindow *ui;

QListWidgetItem *itm;

AskDialog *dialog;

};

and this is the .cpp file of UmerWIndow

#include "umerwindow.h"

#include "ui_umerwindow.h"

#include "askdialog.h"

#include <QListWidgetItem>

UmerWindow::UmerWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::UmerWindow)

{

ui->setupUi(this);

dialog= new AskDialog(this);

}

UmerWindow::~UmerWindow()

{

delete ui;

}

void UmerWindow::on_actionAdd_Item_triggered()

{

dialog->show();

}

void UmerWindow::showlist(QString &data)

{

}

What should i do to resolve this problem? Please help.

PS: I also tried without inheritance, then the compiler gives me a weird error: ISO C++ forbids declaration of 'UmerWindow' with no type and error: expected ';' before '' token* when i create an pointer object of UmerWindow in askdialog.h file. Now what should i do?

网友答案:

According to you UmerWindow is basically a typical MainWindow that Qt creator makes, and u might be knowing the QMainWindow inherits QObject and same is the case for QDialog.

So, class AskDialog inherits from two QObject subclasses QDialog and UmerWindow. This is not supported!

网友答案:

Looks like a classic Circular reference problem. You've done this in the umerwindow.h file.

#include "askdialog.h"

The problem is you've done this before forward declaring UmerWindow. As such the #include "umerwindow.h" in askdialog.h has no affect (due to #define already created).

Instead of this #include, please just forward declare the class AskDialog in umerwindow.h.

网友答案:

error: expected class-name before '{' token

From my remote error resolving skills, I feel you are giving wrong class name while inheriting. Check whether the class name is UmerWindow or umerWindow or umerwindow or Umerwindow. Compiler is not understanding the identifier UmerWindow in your case while inheriting. Go to the header "umerwindow.h" and see what exactly is the class name.

网友答案:

It seems the compiler doesn't know the class UmerWindow. Have you tried forward-declaring it? Just add the class definition below the include statement:

#include "umerwindow.h"    
class UmerWindow;

Also if it's a class created by Qt, it might be in the Ui namespace. In that case, try using the full name in the class declaration:

class AskDialog : public QDialog, public Ui::UmerWindow
分享给朋友:
您可能感兴趣的文章:
随机阅读: