PDA

View Full Version : #include class



mickey
10th June 2006, 19:59
Hi, I've done this thing; I wonder if could arise a problem...can I avoid _myw with a simple parent() ??? thanks


#include "mymainform.h"
myLightDialog::myLightDialog( QWidget* parent, const char* name, bool modal, WFlags fl )
:lightDialog (parent, name, modal, fl) {
_myw = (myMainForm*) parent;



#include <myLightDialog.h>
myMainForm::myMainForm( QWidget* parent, const char* name, WFlags fl )
: MainForm( parent, name, fl )
{
lightd = new myLightDialog(this);

Michiel
10th June 2006, 20:25
I don't understand your question exactly, but you're probably worried about the recursive inclusion. Instead of including it in mylightdialog.h, you can predeclare the myMainForm class and then really include it in the .cpp/.cc file. So it would become:


class myMainForm;

myLightDialog::myLightDialog( QWidget* parent, const char* name, bool modal, WFlags fl )
:lightDialog (parent, name, modal, fl) {
_myw = (myMainForm*) parent;

wysota
10th June 2006, 21:41
Hi, I've done this thing; I wonder if could arise a problem...can I avoid _myw with a simple parent()
No, because parent() is of type QWidget* and _myw is of type myMainForm*. You can use parent() but you'll have to cast it to myMainForm* each time you want to use any of myMainForm methods. But you may define such a method in your class:


inline myMainForm* myLightDialog::formParent(){ return dynamic_cast<myMainForm*>(parent()); }

and use formParent() instead of parent().

mickey
11th June 2006, 08:58
thanks, but I worried for recursive inclusion; I already inserted "class myMainForm" in myLightDialog Class (.h); that's the .cpp; Don't I have to insert "#include <mymainform.h> there" (in the .cpp)?

Michiel
11th June 2006, 09:23
Yes, include mymainform.h into mylightdialog.cpp. In mydialog.h you only needed the name. In the .cpp you need the actual class (probably).

mickey
11th June 2006, 11:11
Yes, include mymainform.h into mylightdialog.cpp. In mydialog.h you only needed the name. In the .cpp you need the actual class (probably).
sorry but #include "mymainfrom.h" in .cpp seems necessary....don't compile with only class mymainform...
these are the errors:


myLightDialog.cpp(27): error C2027: use of undefined type 'myMainForm'
myLightDialog.cpp(27): error C2227: left of '->isEnabled' must point to class/struct/union
myLightDialog.cpp(27): error C2227: left of '->tab1' must point to class/struct/union
myLightDialog.cpp(28): error C2027: use of undefined type 'myMainForm'

Michiel
11th June 2006, 12:36
Yes, it is necessary, like I said.

In the .h file you only use 'class myMainForm'. In the .cpp file you use '#include "mymainform.h"'.

This way you won't get compiler errors, and you won't have recursive inclusion (which would only have happened with '#include ...' in the .h file).