confusing compiling error
I made a simple main file to test some things :
Code:
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtCore/QObject>
class application;
class UI {
public:
UI( application* a) {
app=a;
connect( button, SIGNAL( clicked() ), this, SLOT( exit()) );
frame -> show();
}
public slots:
void exit() {
app -> exit();
}
private:
application* app;
};
class application {
public:
application( int argc, char** argv) {
ui = new UI( this);
qapp -> exec();
}
void exit( int code = 0 ){
qapp -> exit( code );
exit( code );
}
private:
UI* ui;
};
int main( int argc, char** argv ){
application( argc, argv );
return 0;
}
And this are the compile errors i get:
Code:
main.cpp: In constructor ‘UI::UI(application*)’:
main.cpp:16: error: ‘connect’ was not declared in this scope
main.cpp: In member function ‘void UI::exit()’:
main.cpp:21: error: invalid use of incomplete type ‘struct application’
main.cpp:6: error: forward declaration of ‘struct application’
I'm really confused about what the problem could be.
hannesvdc
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.
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
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.
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:
Code:
main.cpp: In member function ‘void UI::exit()’:
main.cpp:30: error: invalid use of incomplete type ‘struct application’
main.cpp:6: error: forward declaration of ‘struct application’
This is the code:
Code:
#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtCore/QObject>
class application;
Q_OBJECT
public:
UI( application* );
public slots:
void exit();
private:
application* app;
};
UI::UI( application* a ){
app = a;
connect( button, SIGNAL( clicked() ), this, SLOT( exit()) );
frame -> show();
}
void UI::exit( ) {
app->exit();
}
/*******************************************************************/
class application {
public:
application( int , char** ) ;
void exit( int = 0 );
private:
UI* ui;
};
application::application( int argc, char** argv) {
ui = new UI( this);
qapp -> exec();
}
void application::exit( int code ){
qapp->exit(code);
exit(code);
}
/******************************************************************/
int main( int argc, char** argv ){
application( argc, argv );
return 0;
}
hannesvdc
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.