Results 1 to 5 of 5

Thread: Two class, two headers files - one problem.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2013
    Location
    Poland
    Posts
    15
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Question Two class, two headers files - one problem.

    Qt Code:
    1. #ifndef FORMCLASS1_H
    2. #define FORMCLASS1_H
    3.  
    4. #include <QWidget>
    5. #include "formclass2.h"
    6.  
    7. namespace Ui {
    8. class FormClass1;
    9. }
    10.  
    11. class FormClass1 : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit FormClass1(QWidget *parent = 0);
    17. ~FormClass1();
    18.  
    19. void test1(FormClass2 *ttee);
    20. private:
    21. Ui::FormClass1 *ui;
    22. };
    23.  
    24. #endif // FORMCLASS1_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef FORMCLASS2_H
    2. #define FORMCLASS2_H
    3.  
    4. #include <QWidget>
    5.  
    6. #include "formclass1.h"
    7.  
    8. namespace Ui {
    9. class FormClass2;
    10. }
    11.  
    12. class FormClass2 : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit FormClass2(QWidget *parent = 0);
    18. ~FormClass2();
    19.  
    20.  
    21. void test2(FormClass1 *test222);
    22.  
    23. private:
    24. Ui::FormClass2 *ui;
    25. };
    26.  
    27. #endif // FORMCLASS2_H
    To copy to clipboard, switch view to plain text mode 

    Hi, I want to create a program that will consist of two classes.
    I want to do something like this:
    http://stackoverflow.com/questions/1...nother-class-c

    but there is a problem:
    /home/hans/QT_projects/Classtest/formclass2.h:21: błąd:'FormClass1' has not been declared


    Please help me.

  2. #2
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Two class, two headers files - one problem.

    You've got a cross-inclusion. Do it with forward declarations instead and put the includes in your cpp files:

    Qt Code:
    1. #ifndef FORMCLASS1_H
    2. #define FORMCLASS1_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class FormClass1;
    8. }
    9.  
    10. class FormClass2;
    11.  
    12. class FormClass1 : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit FormClass1(QWidget *parent = 0);
    18. ~FormClass1();
    19.  
    20. void test1(FormClass2 *ttee);
    21. private:
    22. Ui::FormClass1 *ui;
    23. };
    24.  
    25. #endif // FORMCLASS1_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef FORMCLASS2_H
    2. #define FORMCLASS2_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class FormClass2;
    8. }
    9.  
    10. class FormClass1;
    11.  
    12. class FormClass2 : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit FormClass2(QWidget *parent = 0);
    18. ~FormClass2();
    19.  
    20.  
    21. void test2(FormClass1 *test222);
    22.  
    23. private:
    24. Ui::FormClass2 *ui;
    25. };
    26.  
    27. #endif // FORMCLASS2_H
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChiliPalmer for this useful post:

    h-n-s (29th March 2013)

  4. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Two class, two headers files - one problem.

    If you have problem like this it usually points to a design problem. You should be able to refactor your code to remove circular references (I virtually guarantee that it's possible).
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Two class, two headers files - one problem.

    If you have problem like this it usually points to a design problem.
    I sort of agree with this, but sometimes is really is necessary for two classes to know something about each other. But typically, the two classes don't actually need pointers to each other, what they need is to pass information between themselves. In Qt, this is usually done with signals and slots. Each class has a signal "sendSomeInformation( const SomeInfo & )" and a slot "receiveSomeInformation( const SomeInfo & )" which are cross-connected (in a way that doesn't cause infinite recursion). When Class1 changes the information, it emits the signal, which Class2 receives and can update its internal state accordingly. Likewise, the same thing happens when Class2 changes the state of the information. If there is more information that needs to be shared, either make a bigger (less granular) shared object or implement multiple signal / slot pairs.

    Note that in this case, neither Class1 or Class2 know anything about the other, and don't need to; all they both know about is the "SomeInfo" class they pass back and forth. Likewise, the class that constructs the instances of Class1 and Class2 only needs to know that each of them has certain signals and slots that need to be connected, but that class doesn't need to know anything more about "SomeInfo" except that a reference will be passed in the connection.

    The nice part about this from a design point of view is that you could completely replace Class1 or Class2 with a totally new implementation, and the opposite partner wouldn't know or care, so long as the signal, slot, and "SomeInfo" remained the same. The only class that would care is the one that constructs the two class instances.

  6. #5
    Join Date
    Mar 2013
    Location
    Poland
    Posts
    15
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Two class, two headers files - one problem.

    Quote Originally Posted by d_stranz View Post
    I sort of agree with this, but sometimes is really is necessary for two classes to know something about each other. But typically, the two classes don't actually need pointers to each other, what they need is to pass information between themselves. In Qt, this is usually done with signals and slots. Each class has a signal "sendSomeInformation( const SomeInfo & )" and a slot "receiveSomeInformation( const SomeInfo & )" which are cross-connected (in a way that doesn't cause infinite recursion). When Class1 changes the information, it emits the signal, which Class2 receives and can update its internal state accordingly. Likewise, the same thing happens when Class2 changes the state of the information. If there is more information that needs to be shared, either make a bigger (less granular) shared object or implement multiple signal / slot pairs.

    Note that in this case, neither Class1 or Class2 know anything about the other, and don't need to; all they both know about is the "SomeInfo" class they pass back and forth. Likewise, the class that constructs the instances of Class1 and Class2 only needs to know that each of them has certain signals and slots that need to be connected, but that class doesn't need to know anything more about "SomeInfo" except that a reference will be passed in the connection.

    The nice part about this from a design point of view is that you could completely replace Class1 or Class2 with a totally new implementation, and the opposite partner wouldn't know or care, so long as the signal, slot, and "SomeInfo" remained the same. The only class that would care is the one that constructs the two class instances.
    thx for msg.
    I just solved this problem by using signals and slots mechanism.

Similar Threads

  1. Problem with SVG libraries and headers
    By mchome in forum Newbie
    Replies: 4
    Last Post: 20th August 2012, 20:56
  2. Replies: 3
    Last Post: 28th April 2011, 07:36
  3. Replies: 0
    Last Post: 9th December 2009, 06:34
  4. QTableView Headers problem
    By giusepped in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2009, 11:05
  5. QMake / headers / lot of files
    By jcr in forum Qt Programming
    Replies: 7
    Last Post: 10th January 2006, 12:06

Tags for this Thread

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.