PDA

View Full Version : confusing compiling error



hannesvdc
22nd December 2010, 21:45
I made a simple main file to test some things :

#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtCore/QObject>

class application;


class UI {

public:
UI( application* a) {
app=a;
frame = new QMainWindow;
button = new QPushButton( frame );
connect( button, SIGNAL( clicked() ), this, SLOT( exit()) );
frame -> show();
}
public slots:
void exit() {
app -> exit();
}
private:
application* app;
QMainWindow* frame;
QPushButton* button;
};


class application {
public:
application( int argc, char** argv) {
qapp = new QApplication( argc, argv);
ui = new UI( this);
qapp -> exec();
}

void exit( int code = 0 ){
qapp -> exit( code );
exit( code );
}
private:
QApplication* qapp;
UI* ui;
};


int main( int argc, char** argv ){
application( argc, argv );
return 0;
}

And this are the compile errors i get:

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

Lykurg
22nd December 2010, 21:49
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.

hannesvdc
22nd December 2010, 21:57
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

jdiewald
22nd December 2010, 22:23
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.

hannesvdc
23rd December 2010, 06:53
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:
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:

#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtCore/QObject>

class application;


class UI : public QObject {
Q_OBJECT
public:
UI( application* );
public slots:
void exit();
private:
application* app;
QMainWindow* frame;
QPushButton* button;
};

UI::UI( application* a ){
app = a;
frame = new QMainWindow;
button = new QPushButton( frame );
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:
QApplication* qapp;
UI* ui;
};

application::application( int argc, char** argv) {
qapp = new QApplication( argc, 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

Lykurg
23rd December 2010, 07:12
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.