-
December 20th, 2010, 02:18 AM
#1
Getting error invalid use of incomplete type ‘struct
Hi to all,
Hope you all will be fine. Actually i wrote a simple class in which i used inheritance. But when i instantiate this class in main() i am getting error
invalid use of incomplete type ‘struct MainScreen'
I used the forward declaration and include the parent class header too in my main file but i am still getting the error. Here is my code
Code:
#include <QtGui/QApplication>
#include <QPalette>
#include "mainwindow.h"
#include <QtGui>
#include "Helper.h"
#include <qgridlayout.h>
class MainScreen;
int main(int argc, char *argv[])
{
MainScreen * m = new MainScreen(); //error
}
MainScreen.cpp
Code:
#include <QtGui>
class MainScreen : public QGridLayout
{
public:
MainScreen() {
QToolButton *campaign_btn = new QToolButton();
campaign_btn->setText("Germany");
addWidget(campaign_btn,0,0,0);
}
};
Please tell me why i am getting this error.
Thanks
-
December 20th, 2010, 05:20 AM
#2
Re: Getting error invalid use of incomplete type ‘struct
Originally Posted by Basit781
Hi to all,
Hope you all will be fine. Actually i wrote a simple class in which i used inheritance. But when i instantiate this class in main() i am getting error
You don't need all that code to duplicate the error:
Code:
class MainScreen;
int main()
{
MainScreen *p = new MainScreen();
}
I used the forward declaration and include the parent class header too in my main file but i am still getting the error. Here is my code
No one except yourself knows what's in those headers. They are black boxes as far as anyone else is concerned when looking at your code. Obviously all of those #includes fails to include the full definition of the MainScreen class, so it's your job to figure out why.
To prove it, this code compiles:
Code:
class MainScreen
{
}; // here is a full definition
class MainScreen;
int main()
{
MainScreen *p = new MainScreen(); // works
}
Also, you don't need to use "new" to create a MainScreen object. C++ is not Java or C#
Code:
class MainScreen
{
}; // here is a full definition
int main()
{
MainScreen p; // works
}
Regards,
Paul McKenzie
-
December 20th, 2010, 05:37 AM
#3
-
December 20th, 2010, 08:17 AM
#4
Re: Getting error invalid use of incomplete type ‘struct
you might want to reconsider that inheritance because any layout is not a QWidget, therefore it is never painted. layouts are 'given' to a widget in order to be presented on screen
-
December 21st, 2010, 03:17 AM
#5
Re: Getting error invalid use of incomplete type ‘struct
It's better to split code into header and source files!
-
December 21st, 2010, 03:49 AM
#6
Re: Getting error invalid use of incomplete type ‘struct
yup It was just a trial exercise
-
December 21st, 2010, 06:13 AM
#7
Re: Getting error invalid use of incomplete type ‘struct
Originally Posted by Basit781
Hi,
Thanks Mr Paul i always wait for you when i put post here in C++ forums . Anyways when i did it in this way it compiled. Actually MainScreen is .cpp file. But when i converted it to .h file then it compiled fine. I think i was doing wrong. I should use .h file instead of .cpp file.
Code:
#include "MainScreen.h"
int main()
{
MainScreen *p = new MainScreen; // i used new(on heap) just like that, it can also be MainScreen mainScreen;(on stack);
}
MainScreen.h
Code:
class MainScreen : public QGridLayout
{
public:
MainScreen() {
QToolButton *campaign_btn = new QToolButton();
campaign_btn->setText("Germany");
addWidget(campaign_btn,0,0,0);
}
};
Thanks
Actually your explanations are confused and most likely wrong. What do you mean by 'converted it to .h ... and then it compiled fine' ????
A .h file will not get compiled beside it was included from a .cpp file. Also you can't just rename files and expect that the project still builds correctly. Normally you have to update a makefile or change the project settings of an IDE after such changes.
As a general line a .h file should only hold declarations but not implementations. There are exceptions to this rule, e. g. for template classes/functions or for high-performance code with inline optimization, but I can't see that for your case. You do the declarations into the .h and do the implementations into a .cpp of the same name including the .h file at top. Then you have to add the .cpp file to your project (if using an IDE you also can add the header to the project but that is only for easier access in the IDE). Then, for using the class you include the header (and only the header) in the cpp or header where you use it. In a header forward declarations can be used instead of including when you use the class type only with pointers (*) or reference (&) as arguments in function declarations or as members. You never will use a forward declaration in a .cpp beside you would use no headers at all and have some mutually referencing class definitions.
If you apply the rule above you will get the error 'incomplete type' only for one case where you were including a.h for class A where class B only was declared forwardly and then use the B type somehow in the cpp. To resolve from that you simply have to include the b.h . So, your initial problem probably was because the mainscreen.h was empty or because a second (empty)mainscreen.h was included. The additional forward declaration in the cpp - though needless - must not cause the error at least not for actual C++ compilers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|