Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

error: multiple definition of `MemberExists::on_viewMemberBtn_clicked()'



  • I don't seem to find what is causing this error but here is what I was trying to get done.

    Create a signal/slot and connect them between MainWindow.cpp (main window) and MemberExists.cpp (QDialog).

    The error is inside moc_memberexists.cpp, I didn't write this file so I can only guess. SIGNAL 1 seems to be the one causing the problem

    // SIGNAL 0
    void MemberExists::displayExistingMember()
    {
        QMetaObject::activate(this, &staticMetaObject, 0, Q_NULLPTR);
    }
    
    // SIGNAL 1
    void MemberExists::on_viewMemberBtn_clicked()
    {
        QMetaObject::activate(this, &staticMetaObject, 1, Q_NULLPTR);
    }
    

    I looked online and the one solution I found was that the header file might have been included in multiple places. When I commented out the #include "memberexists.h" in mainWindow.cpp, I got the following error:

    error: 'MemberExists' was not declared in this scope
    MemberExists memExists;
    ^


  • Moderators

    Somehow you mixed up signals and slots in your MemberExists class.

    The signature void MemberExists::on_viewMemberBtn_clicked() looks like the automatically generated slot when you use the "go to slot" feature in the designer. It should put a declaration like this in your MemberExists.h file:

    private slots:
        void on_viewMemberBtn_clicked();
    

    On the other hand the comment seems to indicate that it's an automatically generated body of a signal which means you have a declaration like this:

    signals:
        void on_viewMemberBtn_clicked();
    

    Maybe you or someone else edited the header and the slot fell into signals section.
    Anyway, to fix this put the declaration in the header in the private slots: section.


  • Lifetime Qt Champion

    Hi,

    on_viewMemberBtn_clicked should be a slot, did you declare it as a signal and implemented it in your class ?



  • Is this a bug in Qt Designer? Or just me?

    I'm the only one working on this project and I use the designer.....I didn't input the viewMemberBtn manually. I right clicked on the button and choose go to, etc.

    I also tried the following and it worked but need your input if this is another way to fix the problem or it could be a new problem to introduce.

    I commented out the following section which was causing the errors in the moc_memberexists.cpp:

    /*
    // SIGNAL 1
    void MemberExists::on_viewMemberBtn_clicked()
    {
    QMetaObject::activate(this, &staticMetaObject, 1, Q_NULLPTR);
    }
    */

    the program compiled and ran fine and the part that uses the signal/slot and the button, viewMemberBtn, worked fine and produced the correct result.

    Note: when I tried the above I didn't change

    signals:
    void on_viewMemberBtn_clicked();

    to

    private slots:
    void on_viewMemberBtn_clicked();


  • Moderators

    This is a bug in your code, not in Qt. A really ugly bug I might add.

    Your solution seems to "work" because signals and slots are just regular c++ functions, so whether they are in signals or slots section you can connect to them. That is as long as there's only one implementation of it. When you use the "go to slot" function it creates an implementation in your cpp file. When you then move it to signals section it creates another implementation in the moc_* file. That won't compile. You commented out one of the definitions so it compiled. But the moc_* file will be regenerated next time you touch the ui in the designer so your workaround will be gone and it will again stop to work.

    Don't lie to your tools. Don't put slots in signals sections and then look for workarounds. That's just bad.
    The only valid solution is to put on_viewMemberBtn_clicked() in the slots section where it belongs. It is a slot, so putting it in signals section just confuses the tooling and is a clear bug on your side.


Log in to reply