Results 1 to 6 of 6

Thread: confusing compiling error

  1. #1
    Join Date
    Sep 2010
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default confusing compiling error

    I made a simple main file to test some things :
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui/QMainWindow>
    3. #include <QtGui/QPushButton>
    4. #include <QtCore/QObject>
    5.  
    6. class application;
    7.  
    8.  
    9. class UI {
    10.  
    11. public:
    12. UI( application* a) {
    13. app=a;
    14. frame = new QMainWindow;
    15. button = new QPushButton( frame );
    16. connect( button, SIGNAL( clicked() ), this, SLOT( exit()) );
    17. frame -> show();
    18. }
    19. public slots:
    20. void exit() {
    21. app -> exit();
    22. }
    23. private:
    24. application* app;
    25. QMainWindow* frame;
    26. QPushButton* button;
    27. };
    28.  
    29.  
    30. class application {
    31. public:
    32. application( int argc, char** argv) {
    33. qapp = new QApplication( argc, argv);
    34. ui = new UI( this);
    35. qapp -> exec();
    36. }
    37.  
    38. void exit( int code = 0 ){
    39. qapp -> exit( code );
    40. exit( code );
    41. }
    42. private:
    43. QApplication* qapp;
    44. UI* ui;
    45. };
    46.  
    47.  
    48. int main( int argc, char** argv ){
    49. application( argc, argv );
    50. return 0;
    51. }
    To copy to clipboard, switch view to plain text mode 

    And this are the compile errors i get:
    Qt Code:
    1. main.cpp: In constructor ‘UI::UI(application*)’:
    2. main.cpp:16: error: ‘connect’ was not declared in this scope
    3. main.cpp: In member function ‘void UI::exit()’:
    4. main.cpp:21: error: invalid use of incomplete type ‘struct application’
    5. main.cpp:6: error: forward declaration of ‘struct application’
    To copy to clipboard, switch view to plain text mode 

    I'm really confused about what the problem could be.

    hannesvdc

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: confusing compiling error

    Use the Q_OBJECT macro, inherit from a QObject class or use QObject::connect().

    And to use the pointer application your need to declare the class members, declaring only the class is not enough.

  3. #3
    Join Date
    Sep 2010
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: confusing compiling error

    I'm sorry, but i don't understand your answer quite well.
    Where do i have to put Q_OBJECT and what do you mean by

    'your need to declare the class members, declaring only the class is not enough.'?

    hannesvdc

  4. #4
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: confusing compiling error

    1. Signals and slots - read http://doc.qt.nokia.com/4.6/signalsandslots.html carefully. The UI class wants to use slots, so it must be based on QObject.

    2. By declaring "class application" before the definition of the UI class, you've only told the compiler enough to compile constructs like pointer assignments. However, in your exit function, you try to dereference the application object pointer and make a call to the exit method. The compiler hasn't seen a class declaration containing a definition of the exit function, so it can't know enough to generate code. The full definition of the application class would have to come first. Unfortunately, you can't do that because the application class needs the definition of the UI class in order to know about the UI constructor. You have a circle of references.

    You'll need to move the bodies of the methods outside of the class declarations in order to break your circle.

  5. #5
    Join Date
    Sep 2010
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: confusing compiling error

    I did what you told, and the errors that had to do with 'connect' are gone. I also moved the
    function bodies outside the class declarations, but i still get this error:
    Qt Code:
    1. main.cpp: In member function ‘void UI::exit()’:
    2. main.cpp:30: error: invalid use of incomplete type ‘struct application’
    3. main.cpp:6: error: forward declaration of ‘struct application’
    To copy to clipboard, switch view to plain text mode 

    This is the code:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui/QMainWindow>
    3. #include <QtGui/QPushButton>
    4. #include <QtCore/QObject>
    5.  
    6. class application;
    7.  
    8.  
    9. class UI : public QObject {
    10. Q_OBJECT
    11. public:
    12. UI( application* );
    13. public slots:
    14. void exit();
    15. private:
    16. application* app;
    17. QMainWindow* frame;
    18. QPushButton* button;
    19. };
    20.  
    21. UI::UI( application* a ){
    22. app = a;
    23. frame = new QMainWindow;
    24. button = new QPushButton( frame );
    25. connect( button, SIGNAL( clicked() ), this, SLOT( exit()) );
    26. frame -> show();
    27. }
    28.  
    29. void UI::exit( ) {
    30. app->exit();
    31. }
    32. /*******************************************************************/
    33. class application {
    34. public:
    35. application( int , char** ) ;
    36.  
    37. void exit( int = 0 );
    38. private:
    39. QApplication* qapp;
    40. UI* ui;
    41. };
    42.  
    43. application::application( int argc, char** argv) {
    44. qapp = new QApplication( argc, argv);
    45. ui = new UI( this);
    46. qapp -> exec();
    47. }
    48.  
    49. void application::exit( int code ){
    50. qapp->exit(code);
    51. exit(code);
    52. }
    53. /******************************************************************/
    54. int main( int argc, char** argv ){
    55. application( argc, argv );
    56. return 0;
    57. }
    To copy to clipboard, switch view to plain text mode 

    hannesvdc

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: confusing compiling error

    beside that your code looks crude and I don't really know what you are trying to do: Put the definition of application in front of the class UI.

Similar Threads

  1. Confusing bug with threaded application
    By kachofool in forum Newbie
    Replies: 1
    Last Post: 15th December 2009, 23:32
  2. Replies: 1
    Last Post: 25th October 2008, 19:18
  3. Confusing SIGSEV
    By mtrpoland in forum Newbie
    Replies: 5
    Last Post: 5th September 2007, 12:07
  4. Confusing in thread implementation
    By santosh.kumar in forum Qt Programming
    Replies: 0
    Last Post: 16th May 2007, 11:07

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.