Results 1 to 5 of 5

Thread: 2 classes header file inclusion probleml !!

  1. #1
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default 2 classes header file inclusion probleml !!

    Dear Friends
    I am finding difficulty this is a Qt error but I think the problem is C++ related.

    I am getting these errors when I am including a header file (a class) whose object I want to access from some other class ..

    These two classes
    'meshparameterdlg.h' has the class 'MeshParameterDlg'
    'controlPointsEditorDlg.h' has the class 'ControlPointsEditorDlg'
    Both the classes are inherited by QDialog.

    I am including 'controlPointsEditorDlg.h' in 'meshparameterdlg.h' and
    'meshparameterdlg.h' in 'controlPointsEditorDlg.h' .

    I want to have 'MeshParameterDlg' object as a data member of 'ControlPointsEditorDlg' and
    'ControlPointsEditorDlg' object as a data member of 'MeshParameterDlg'.

    when I am adding 'controlPointsEditorDlg.h' in 'meshparameterdlg.h' then there's no problem. When user presses a button from 'MeshParameterDlg' dialog then I am instantiating another dialog class that is 'ControlPointsEditorDlg' and this dialog appears and when user presses 'ok' button on 'ControlPointsEditorDlg' class then I should get some values from the QTextEdit from 'ControlPointsEditorDlg' class, so I want 'meshparameterdlg.h' to be included in 'controlPointsEditorDlg.h' so that I can put the value from the 2nd dialog to a member in the 1st dialog.

    When I am adding 'meshparameterdlg.h' in 'controlPointsEditorDlg.h' I am getting these errors.

    Please see below errors.
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    In file included from src/../include/mainwindow.h:35,
    from src/../include/graphicsscene.h:19,
    from src/../include/graphicsview.h:19,
    from src/../include/geometry.h:25,
    from src/../include/checksmoothness.h:26,
    from src/checksmoothness.cpp:14:
    src/../include/meshparameterdlg.h:11: error: redefinition of `class MeshParameterDlg'
    src/../include/meshparameterdlg.h:11: error: previous definition of `class MeshParameterDlg'
    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    But I am not doing anythin in 'meshparameterdlg.h' at line #11. But I am adding meshparameterdlg.h header in a separate class called 'controlPointsEditorDlg.h'
    in both the header file i am adding
    #ifndef HEADER_H
    #define HEADER_H

    class definition

    #endif

    Could you give me some solution to this problem I am fed up for the past 2 days with these.

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 2 classes header file inclusion probleml !!

    Could you show us these headers' code?
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default Re: 2 classes header file inclusion probleml !!

    I am out of office for today tomorrow I'll put these two heaeder files see if you could help me to get rid of it !

    Thanks & Regards
    Sujan Dasmahapatra.

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 2 classes header file inclusion probleml !!

    I am including 'controlPointsEditorDlg.h' in 'meshparameterdlg.h' and
    'meshparameterdlg.h' in 'controlPointsEditorDlg.h'
    This is rather bad idea. What you have to do is to include at least one of these headers to the .cpp file, for example imagine 4 files: a.h, a.cpp, b.h, b.cpp - with classes A and B.
    If you want to have in class A a pointer member to class B you have to use "forward declaration":
    a.h:
    Qt Code:
    1. #ifndef A_H
    2. #define A_H
    3.  
    4. class B;
    5. class A
    6. {
    7. B *b;
    8. };
    9. #endif
    To copy to clipboard, switch view to plain text mode 
    a.cpp:
    Qt Code:
    1. #include "a.h"
    2. #include "b.h"
    3.  
    4. // . . . some code for class A using class B as well
    To copy to clipboard, switch view to plain text mode 
    b.h
    Qt Code:
    1. #ifndef B_H
    2. #define B_H
    3.  
    4. class A;
    5. class B
    6. {
    7. A *a;
    8. };
    9. #endif
    To copy to clipboard, switch view to plain text mode 
    b.cpp
    Qt Code:
    1. #include "a.h"
    2. #include "b.h"
    3.  
    4. // . . . some code for B using A as well
    To copy to clipboard, switch view to plain text mode 
    You can do it for just A or just B not for both and it should ok too. The only thing is that when you use forward declaration you can only use the pointer to the forward declared class, you can't use the whole object, so this is wrong:
    Qt Code:
    1. class A;
    2. class B
    3. {
    4. A a;
    5. };
    To copy to clipboard, switch view to plain text mode 
    if you don't include a.h.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 2 classes header file inclusion probleml !!

    src/../include/meshparameterdlg.h:11: error: redefinition of `class MeshParameterDlg'
    src/../include/meshparameterdlg.h:11: error: previous definition of `class MeshParameterDlg'

    Certainly looks like a file is being included twice, trying to redefine the same class, and thus causing the compiler to hate you. Are you sure your include guards are working?

Similar Threads

  1. Problems with nmake: vc90.pdb header file incorrect
    By deville75 in forum Installation and Deployment
    Replies: 1
    Last Post: 28th July 2009, 23:00
  2. 'QFile' Header File Problem
    By hasnatzaidi in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 22:40
  3. dll file inclusion
    By omprakash in forum Qt Programming
    Replies: 3
    Last Post: 31st July 2008, 20:41
  4. AVOIDING mutiple inclusion of header file???
    By pratik in forum General Programming
    Replies: 4
    Last Post: 10th July 2007, 15:09
  5. How to search a string in xml file with Qt Dom classes
    By doganay44 in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2006, 21:16

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.