Results 1 to 15 of 15

Thread: problem with forward declaration

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Red face problem with forward declaration

    Hi to all!

    I have some .h file with:

    Qt Code:
    1. class ForwardDeclarationClass; // forward declaration of class
    To copy to clipboard, switch view to plain text mode 

    In same header file there is:
    Qt Code:
    1. class MyClass : public QGlWidget
    2. {
    3. ....
    4. private:
    5. ForwardDeclarationClass* d;
    6. }
    To copy to clipboard, switch view to plain text mode 

    In the .cpp file I have:
    Qt Code:
    1. ...
    2. ...
    3. ...
    4. class ForwardDeclarationClass
    5. {
    6. ....
    7. ....
    8. }
    To copy to clipboard, switch view to plain text mode 

    and after that there are defined all corresponding methods (in same .cpp file).

    I try to compile this Qt project and I get following error:
    Qt Code:
    1. forward declaration of `struct ForwardDeclarationClass'
    To copy to clipboard, switch view to plain text mode 

    What is wrong?
    Qt 5.3 Opensource & Creator 3.1.2

  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: problem with forward declaration

    Could you post the whole error message?

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with forward declaration

    Here is it:
    Qt Code:
    1. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    To copy to clipboard, switch view to plain text mode 

    Namely:
    - CMerchandizeBrowser is MyClass
    - CMerchandizeBrowserPrivate is ForwardDeclarationClass
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    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: problem with forward declaration

    I suspect you're using a non-pointer or non-reference in the header. In this case a forward declaration is insufficient. Could this be the case? What does CMerchandizeBrowser.h:34 contain?
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with forward declaration

    Here is line:
    Qt Code:
    1. class CMerchandizeBrowserPrivate; // forward declaration
    To copy to clipboard, switch view to plain text mode 
    And later in the same file in the class CMerchandizeBrowser I have:
    Qt Code:
    1. private:
    2. CMerchandizeBrowserPrivate* d;
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  6. #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: problem with forward declaration

    But is that line 34? As you can see from the error message, it comes from line 34.
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with forward declaration

    Like I said, the line 34, where error occurs, is:
    Qt Code:
    1. class CMerchandizeBrowserPrivate; // forward declaration
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  8. #8
    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: problem with forward declaration

    Is that the only error message you get? What happens if you comment out that line #34?

  9. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with forward declaration

    I get several errors after that:
    Qt Code:
    1. CMerchandizeBrowser.cpp: In member function `int CMerchandizeBrowser::slideCount() const':
    2. CMerchandizeBrowser.cpp:372: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    3. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    4. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::setSlideCount(int)':
    5. CMerchandizeBrowser.cpp:377: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    6. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    7. CMerchandizeBrowser.cpp: In member function `QSize CMerchandizeBrowser::slideSize() const':
    8. CMerchandizeBrowser.cpp:382: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    9. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    10. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::setSlideSize(QSize)':
    11. CMerchandizeBrowser.cpp:387: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    12. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    13. CMerchandizeBrowser.cpp: In member function `int CMerchandizeBrowser::zoomFactor() const':
    14. CMerchandizeBrowser.cpp:392: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    15. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    16. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::setZoomFactor(int)':
    17. CMerchandizeBrowser.cpp:397: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    18. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    19. CMerchandizeBrowser.cpp: In member function `virtual QImage CMerchandizeBrowser::slide(int) const':
    20. CMerchandizeBrowser.cpp:402: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    21. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    22. CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::setSlide(int, const QImage&)':
    23. CMerchandizeBrowser.cpp:407: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    24. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    25. CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::setSlide(int, const QPixmap&)':
    26. CMerchandizeBrowser.cpp:412: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    27. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    28. CMerchandizeBrowser.cpp: In member function `int CMerchandizeBrowser::currentSlide() const':
    29. CMerchandizeBrowser.cpp:417: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    30. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    31. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::setCurrentSlide(int)':
    32. CMerchandizeBrowser.cpp:422: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    33. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    34. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::clear()':
    35. CMerchandizeBrowser.cpp:427: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    36. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    37. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::render()':
    38. CMerchandizeBrowser.cpp:432: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    39. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    40. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::showPrevious()':
    41. CMerchandizeBrowser.cpp:438: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    42. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    43. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::showNext()':
    44. CMerchandizeBrowser.cpp:443: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    45. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    46. CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::showSlide(int)':
    47. CMerchandizeBrowser.cpp:448: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    48. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    49. CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::paintEvent(QPaintEvent*)':
    50. CMerchandizeBrowser.cpp:481: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    51. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    52. CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::resizeEvent(QResizeEvent*)':
    53. CMerchandizeBrowser.cpp:486: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    54. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    55. CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::timerEvent(QTimerEvent*)':
    56. CMerchandizeBrowser.cpp:492: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    57. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    58. CMerchandizeBrowser.cpp:493: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
    59. CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
    60. mingw32-make[1]: *** [debug/CMerchandizeBrowser.o] Error 1
    61. mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/workspace/eROSystem'
    62. mingw32-make: *** [debug] Error 2
    To copy to clipboard, switch view to plain text mode 
    but I assumed these errors are invoked because forward class declaration reports error.
    Qt 5.3 Opensource & Creator 3.1.2

  10. #10
    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: problem with forward declaration

    It looks like you are missing #include directive in CMerchandizeBrowser.cpp. Where is CMerchandizeBrowserPrivate defined?

  11. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: problem with forward declaration

    CMerchandizeBrowserPrivate is defined in .cpp file. I think I will get some replies that defining class in .cpp file is not ok...
    Qt 5.3 Opensource & Creator 3.1.2

  12. #12
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: problem with forward declaration

    Correct, you cannot define a class in a .cpp file.
    There are 10 people in the world. Those who understand binary and those who don't.

  13. #13
    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: problem with forward declaration

    Quote Originally Posted by MarkoSan View Post
    CMerchandizeBrowserPrivate is defined in .cpp file.
    Is the definition placed before the lines mentioned in error messages?

    Quote Originally Posted by MarkoSan View Post
    I think I will get some replies that defining class in .cpp file is not ok...
    No, there's nothing wrong with it, but you won't be able to use that class outside that .cpp file.

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

    MarkoSan (7th January 2008)

  15. #14
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: problem with forward declaration

    Quote Originally Posted by jacek View Post
    No, there's nothing wrong with it, but you won't be able to use that class outside that .cpp file.
    I stand corrected. Yes you can. I was thinking of the accessability of those classes.
    There are 10 people in the world. Those who understand binary and those who don't.

  16. The following user says thank you to Thomas for this useful post:

    MarkoSan (7th January 2008)

  17. #15
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Talking Re: problem with forward declaration

    Well, I've moved the class definition in .cpp file to the very beginning of the file, it compiles now!!!
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Forward Class declaration ERROR
    By nleverin in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2007, 08:35
  2. Forward declaration with std namespace
    By Raistlin in forum General Programming
    Replies: 2
    Last Post: 5th March 2007, 20:45
  3. Why forward declaration ?
    By probine in forum General Programming
    Replies: 3
    Last Post: 26th January 2007, 18:22
  4. QTextBrowser Problem
    By Cutey in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2007, 10:19
  5. Compilation problem, don't know why
    By yellowmat in forum Newbie
    Replies: 6
    Last Post: 2nd March 2006, 15:36

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.