John M.
John M.

Reputation: 2264

C++ Qt Multiple Definitions

I'm trying to create a simple GUI application (so far) in Qt with C++ using the MinGW compiler. However, the compiler is informing me that I have a multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)' on line 4 of wiimotescouter.cpp. I am using a check to make sure the header isn't included multiple times, but apparently it's not working, and I'm not sure why.

Here's the header file:

#ifndef WIIMOTESCOUTER_H
#define WIIMOTESCOUTER_H

#include <QWidget>

class QLabel;
class QLineEdit;
class QTextEdit;

class WiimoteScouter : public QWidget
{
    Q_OBJECT

public:
    WiimoteScouter(QWidget *parent = 0);

private:
    QLineEdit *eventLine;
};

#endif // WIIMOTESCOUTER_H

And here's the cpp file:

#include <QtGui>
#include "wiimotescouter.h"

WiimoteScouter::WiimoteScouter(QWidget *parent) :
    QWidget(parent)
{
    QLabel *eventLabel = new QLabel(tr("Event:"));
    eventLine = new QLineEdit;

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(eventLabel, 0, 0);
    mainLayout->addWidget(eventLine, 0, 1);

    setLayout(mainLayout);
    setWindowTitle(tr("Wiimote Alliance Scouter"));
}

Lastly, the main.cpp:

#include <QtGui>
#include "wiimotescouter.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    WiimoteScouter wiimoteScouter;
    wiimoteScouter.show();

    return app.exec();
}

Upvotes: 17

Views: 34470

Answers (9)

sweetpea
sweetpea

Reputation: 11

I am a newbie to Qt and my case was that I provided code logic within my signal, whereas the MOC expects signal declarations to be simple function prototypes, without any implementation or code within the curly braces.

Further, any code logic or actions related to the signal should be implemented in the slots that are connected to the signal, or in other member functions of the class.

Upvotes: 1

Cacafave
Cacafave

Reputation: 11

I was struggling with the same error. In my case the mistake was that I was declaring the method under 'signals', while it had to be declared under 'slots'.

Upvotes: 0

Zciurus
Zciurus

Reputation: 838

In my case this was caused by having the function declared globally in a header file.

Upvotes: 1

iammilind
iammilind

Reputation: 69958

For me it was due to Qt's compilation model in Windows using MinGW.

My code compiled perfectly fine for Linux, but for Windows the linker errors were happening for following files:

Message.cpp
Util.cpp

At first, in the .pro file, I couldn't find any similar file names. Then observing keenly I figured out that, the external google protobuf library, which I was compiling along, had some library files inside its folder named as:

message.cc
util.cc

The cases and the extensions were different, but somehow it created mess in the Qt compilation. I just added an underscore to those library files and the things worked fine.

Upvotes: 0

user1408605
user1408605

Reputation: 45

I got this error message when I had my slot declarations listed listed under the signals heading in the header file, rather than the slots one. Another thing for anyone experiencing this error message to check for.

Cut and paste solved the problem and a need to check next time I create Slots manually.

Upvotes: 0

Assimilater
Assimilater

Reputation: 964

This can also happen if you have two .ui files with the same name in different folders. Their corresponding headers are built in the same directory, resulting in one being overwritten. At least that was my problem.

Upvotes: 1

jwernerny
jwernerny

Reputation: 7048

I've seen this happen before when a source file got duplicated in the project (.pro or .pri) file. Check all of the "SOURCES =" and "SOURCES +=" lines in your project file and make sure the cpp file is not in there more than once.

Upvotes: 68

hecvd
hecvd

Reputation: 668

Answer just for reference:

I was including

#include myclass.cpp

instead of

#include myclass.h

Upvotes: 1

Steve
Steve

Reputation: 1800

I don't use MinGW but this sounds like a linker error rather than a compiler error. If this is the case then you should check that the .CPP file is not added to the project twice. I also noticed that the extension is "php", this is very unusual as it should be "cpp".

Upvotes: 2

Related Questions