1,105,625 Community Members

Inheritance Error

Member Avatar
nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
}

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

Member Avatar
ravenous
Practically a Master Poster
682 posts since Jul 2005
Reputation Points: 251 [?]
Q&As Helped to Solve: 112 [?]
Skill Endorsements: 13 [?]
 
0
 

The error that you mention occurs if you don't include QtGui when compiling. If you are using qmake then you might have a line like QT = core In your .pro file, if you change it to: QT = core gui Then the compiler should be able to find the QDialog header file and that error should go away.

Member Avatar
nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Well I think there is no need of using QtGui header file because all the Widgets and buttons etc i am using have there header files included in my program.Though i used this header file in my askdialog.cpp and in umerwindow.cpp but worthless. When do we use QtGui header file and in which file should i add it? whether in main.cpp or .cpp files of classes.

Member Avatar
ravenous
Practically a Master Poster
682 posts since Jul 2005
Reputation Points: 251 [?]
Q&As Helped to Solve: 112 [?]
Skill Endorsements: 13 [?]
 
0
 

I meant that you need to have the directory QtGui in the include path for the compiler, not #include it in a particular file. I'm using the QtCreator IDE, which uses qmake, so I can do the steps that I mentioned in my previous post. If you're not using qmake, then you need to specify the path to the QtGui directory to the compiler some how. For example, using gcc on Linux, you might pass the argument -I/usr/include/qt4/QtGui to the compiler on the command line. If you're using some IDE then you'll have to find out how to specify include paths in that IDE.

Basically, you get the error that you mentioned if the compiler doesn't recognise one of the classes that you're trying to inherit from, so you need to make sure that the compiler can find all the classes that you want it to use. The other possibility is that it can't find UmerWindow for some reason?

Member Avatar
nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

I have created another fresh copy of the program, used forward declarations of the classes but worthless.
I tried the program without inheritance by creating an object of UmerWindow class in AskDialog class but then the IDE gives me the error of error: ISO C++ forbids declaration of 'UmerWindow' with no type and error: expected ';' before '*' token.
Now what should i do? I am really Pissed. I have to make this project as quickly as possible but i am stuck in this ridiculus problem.

Member Avatar
ravenous
Practically a Master Poster
682 posts since Jul 2005
Reputation Points: 251 [?]
Q&As Helped to Solve: 112 [?]
Skill Endorsements: 13 [?]
 
0
 

... the IDE gives me the error of error: ISO C++ forbids declaration of 'UmerWindow' with no type and error: expected ';' before '*' token.

That means that it doesn't recognise UmerWindow as a class name. Have you checked for typos in the header that mean the class is actually a slightly different name?

Now what should i do? I am really Pissed. I have to make this project as quickly as possible but i am stuck in this ridiculus problem.

Coding when mad is impossible. Calm yourself ASAP.

Member Avatar
nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I checked that too. The name is the same as defined in UmerWindow header file. I think there is some other problem.

Ravenous can you suggest me some beginner programs, because I am learning Qt by making programs like this.

BTW I am very greatfull to you for such nice behavior and help.

Member Avatar
nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yaay!!! THe program is compiled successfully. I just removed #include "umerwindow.h" from askdialog.h file and typed class UmerWindow.

Can you please tell me what is meant by forward declaration? And what is the difference between including the header file of a class and the declaration of the itself? I am asking this because i learned OOP c++ by declaring and implementing the class inside the same file containing int main(). So your help will be very precious for me in this regard.

Member Avatar
mike_2000_17
21st Century Viking
4,092 posts since Jul 2010
Reputation Points: 2,271 [?]
Q&As Helped to Solve: 801 [?]
Skill Endorsements: 73 [?]
Moderator
Featured
Sponsor
 
1
 

>>Can you please tell me what is meant by forward declaration?

A forward-declaration is just a way to tell the compiler that there will eventually be a class declared later in the code. It is only a means to stop the compiler from complaining that it doesn't recognize the type or class. Of course, this is only temporary, the actual declaration of the class has to appear eventually (if it is ever used). When there is only a forward-declaration available so far (as the compiler is looking through the code), you only can do very limited uses of the class in question. Because the compiler doesn't know what that class is, only that it is a valid class, you cannot use any of its member functions or anything, you cannot create an object of that class, you cannot have data members of that class or inherit from it, all you can do is declare pointer / reference parameters or data members to an object of that class. Forward-declarations are typically used when you have two classes that depend on each other (and in other more advanced cases). Here is a simple and typical pattern that involves a forward-declaration:

class Cat; // forward-declaration.

class Dog {
  public:
    void ChaseThisCat(Cat& someCat);  //use of forward-decl. to declare a reference parameter.
};

class Cat {
  public:
    void FleeFromThisDog(Dog& someDog); 
};

// Now, Cat has been declared and can be used to implement the member function of Dog:
void Dog::ChaseThisCat(Cat& someCat) {
  //... use Cat class as much as you like.
};

void Cat::FleeFromThisDog(Dog& someDog) {
  //... ditto.
};

>>And what is the difference between including the header file of a class and the declaration of the itself?

Using headers, they are saved in your hard-drive as two files. That's about it. The #include statements are, for all intent and purposes, the same as a copy-paste operation. That is, #include "my_file.h" tells the compiler: "Go and find the file 'my_file.h' and replace this line with the content of that file." So, it just copies the entire content in-place of the include statement. At the end, you get one big source file that the compiler will look through from start to end. Of course, the process is not quite as simple as a copy-paste (there is some bookkeeping done and other things), but these are just things the compiler will do behind the scenes, the behavior is exactly as if you copy-pasted it.

This is why it is so important to have header-guards too, to make sure the same content of the header files don't get copied many times, causing repeating declarations / definitions, which breaks the One Definition Rule (ODR) of C/C++.

The advantages of header files as opposed to everything in one file (main.cpp) are pretty obvious I think. You can actually reuse the header files without having to constantly copy-paste manually. And so on..

There should never be a difference in behavior (by the compiler) between including a header file and copying its content manually. If you fixed your problem by not including the header file and instead declaring the class UmerWindow directly inside this askdialog file, then this is not what fixed the problem, something else is going on (something else was changed). I guarantee that. You should probably look at what is different between the code you added to askdialog.h and what code appears in umerwindow.h, that's how you're gonna understand how the problem disappeared and how to avoid similar problems in the future. Sometimes you change a few insignificant things and it fixes the problem, but it's not magic, there is a reason why the error was fixed and you should investigate to find out what that is, otherwise, this painful experience is bound to occur again (and the "magic" trick that fixed it this time is unlikely to work again).

Member Avatar
ravenous
Practically a Master Poster
682 posts since Jul 2005
Reputation Points: 251 [?]
Q&As Helped to Solve: 112 [?]
Skill Endorsements: 13 [?]
 
0
 

can you suggest me some beginner programs, because I am learning Qt by making programs like this.

I found that C++ GUI Programming with Qt 4 was quite a good book to learn from, with lots of easy-to-follow examples in it.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: