Results 1 to 20 of 20

Thread: subclassing MainForm class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: subclassing MainForm class

    Thanks. The error is:
    Qt Code:
    1. mymainform.h(4): error C2011: 'myMainForm' : 'class' type redefinition
    To copy to clipboard, switch view to plain text mode 
    I need to obtain his pointer here.
    I don't undertand why for MainForm there aren't problems (I declareed pointer to myMainForm in the same way of pointer to MainForm:
    Qt Code:
    1. //mywidget.h
    2. class MyWidget {
    3. MainForm* w;
    4. myMainForm* myw;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: subclassing MainForm class

    Quote Originally Posted by mickey
    mymainform.h(4): error C2011: 'myMainForm' : 'class' type redefinition
    What do you have in 4th line of the mymainform.h class?

    Did you guard all your headers using following code?
    Qt Code:
    1. #ifndef __FILE_NAME_H__
    2. #define __FILE_NAME_H__
    3. ...
    4. #endif // __FILE_NAME_H__
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: subclassing MainForm class

    Now compile successful.
    the line 4 is:
    Qt Code:
    1. class myMainForm : public MainForm
    To copy to clipboard, switch view to plain text mode 
    in mywidget.h I have #include "mymainform.h"
    in mymainform.h I have #include "mywidget.h"
    Is it a cycle!? Is this the cause?
    Thanks
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: subclassing MainForm class

    Quote Originally Posted by mickey
    in mywidget.h I have #include "mymainform.h"
    in mymainform.h I have #include "mywidget.h"
    Is it a cycle!?
    Yes, it is. You can avoid it using forward declaration.

  5. The following user says thank you to jacek for this useful post:

    mickey (8th March 2006)

  6. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: subclassing MainForm class

    sorry,
    what does 'forward declarations' mean?
    Regards

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: subclassing MainForm class

    Quote Originally Posted by mickey
    sorry,
    what does 'forward declarations' mean?
    Ever tried google?

  8. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: subclassing MainForm class

    Hi I tried this and work!
    Qt Code:
    1. #ifndef MYMAINFORM_H
    2. #define MYMAINFORM_H
    3. //#include "mywidget.h"
    4. class MyWidget;
    5.  
    6. class myMainForm : public MainForm
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. myMainForm( QWidget* parent = 0, const char* name = 0, WFlags fl = WType_TopLevel );
    12. ~myMainForm();
    13. MyWidget* myWidget2;
    14. MyWidget* myWidget3;
    15.  
    16. public slots:
    17. void myChangeView(QAction* action);
    18. signals:
    19. void myMainFormUpdate();
    20. };
    21. #endif
    To copy to clipboard, switch view to plain text mode 
    A Question: to toggle include "mywidget.h" I needed to insert #include "mywidget.h" inside mymainform.cpp. Wasn't it the same? Why is better of my previus code (apart avoid cycle problem).
    Thanks
    Regards

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: subclassing MainForm class

    Quote Originally Posted by mickey
    A Question: to toggle include "mywidget.h" I needed to insert #include "mywidget.h" inside mymainform.cpp. Wasn't it the same? Why is better of my previus code (apart avoid cycle problem).
    Thanks
    You should use forward declarations whenever possible. Use includes only when forward declaration is not suitable.

    Forward declaration is adequate for pointers and references. You have to use includes when compiler needs to know the size of a type. This is the case when you inherit from a class or you have an aggregate object as a class member variable.

    By filling your header files with unnecessary includes only generates extra dependencies and slows compiling down for no purpose at all.

Similar Threads

  1. Replies: 3
    Last Post: 27th December 2008, 19:34
  2. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  3. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  4. Subclassing qbutton class
    By jingbo in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2006, 21:15
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.