Click to See Complete Forum and Search --> : Compiling with moc.


phucket_20
July 17th, 2002, 11:09 AM
Hello,

For some reason I can't get my program to compile, I hope maybe someone intelligent can figure this out.

I am running Mandrake 8.2, gcc 3.0.3, and qt 3.0.4, and attempting to compile a QT app using a custom slot. I've been compiling QT apps all day with no problem, but as soon as i define my own slot and hit moc it blows up. This is what I have.

frmMain.h: ( a class declaration file )

#ifndef FRMMAIN_H
#define FRMMAIN_H

#include <qwidget.h>

class frmMain : public QWidget {
Q_OBJECT
public:
frmMain();
public slots:
void MyCustomSlot()
};
#endif

frmMain.cpp ( the cooresponding class definition file )

#include "frmMain.moc"

frmMain::frmMain() {
// default constructor
}

void frmMain::MyCustomSlot() {
exit(1);
}


now.. regardless of what this class necessarily does, i can't get it to compile. first.. i moc'd my header file

moc frmMain.h -o frmMain.moc

No problems.. then i go to compile the cpp into an o:

g++ -I$QTDIR/include -c frmMain.cpp

That's where I get errors. $QTDIR is correct, so my include path points to the right place, but the errors that it spits out (which is about half a page, but to name just a few are )

"In file included from frmMain.cpp

frmMain.moc:30: no 'void frmMain::initMetaObject()' member function declared in class frmMain
frmMain.moc: In member function 'void frmMain::initMetaObject':
frmMain.moc:34: 'badSuperClassWarning' undeclared ( first use in this function )"

it goes on to tell me things like "prototype for QString frmMain::tr(const char*) does not match any in class frmMain", "In static member function 'static QMetaObject* frmMain::staticMetaObject()': no method QMetaObject::new_metadata(), no method QMetaObject::new_metaaccess, struct QMetaData has no member named 'ptr', 'QMember' undeclared" etc etc for a bit.


Can anyone tell me what they think I could be doing wrong? Perhaps I need to reset some environment library variable or something? From the looks of it I think it's not getting something included right, but I'm not sure.

Any help is appreciated. Thank you!

Graham
July 17th, 2002, 11:31 AM
class frmMain : public QWidget {
Q_OBJECT
public:
frmMain();
public slots: // <---- what is this line?
void MyCustomSlot()
};


The line I've indicated is not valid. Is this a typo or could it be the source of your problem?

Also, what does "Q_OBJECT" do? I presume that this is a macro. Does it declare anything that you then need to define?

phucket_20
July 17th, 2002, 12:07 PM
hey Graham,

yes, Q_OBJECT is a macro.

All Qt classes that hold custom signals and slots must mention this macro. the public slots line is not valid by c++ standards, no, but the meta object compiler ( moc ) that gets run before g++, translates this line into valid c++ code. that way it compiles correctly.

Furthermore, i took this sample class straight out of an example from a book, so the coding can't be that far off. :)

Graham
July 17th, 2002, 12:16 PM
Well, without knowing what Q_OBJECT expands to, it's a bit difficult to come up with a definitive answer. However, the errors that you're getting suggest that Q_OBJECT is declaring some functions that you aren't defining in your .cpp file.

Is there another (partner) macro to Q_OBJECT that fills in the missing bits? I ask because Micro$oft use quite a few of these two-part macros in MFC. For example, if you add DECLARE_DYNAMIC to your class definition, you have to add a corresponding IMPLEMENT_DYNAMIC in the .cpp file.

phucket_20
July 17th, 2002, 06:34 PM
good thinking on the double macro, however i checked my literature and the coding looks clean.

i agree more so that the moc is definetely adding some functions that i do not specifically declare in my cpp file.

however.. these functions should be present in OTHER files. i.e. the Qt library files. i'm pretty sure they are.. i'm gonna find exactly which ones though and see if that leads me anywhere. i think what may be the case is i am not properly including something... though that seems odd because i tried making my makefile with "qmake" a utitility that makes makefiles automatically for Qt and still got the same errors. i'll post as to any success though. thanks!

Graham
July 18th, 2002, 04:13 AM
Good luck!