CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2002
    Location
    VA
    Posts
    26

    Compiling with moc.

    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!

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Code:
    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?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    May 2002
    Location
    VA
    Posts
    26
    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.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    May 2002
    Location
    VA
    Posts
    26
    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!

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Good luck!
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured