C++ Qt Multiple Definitions

33.5k views Asked by At

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();
}
7

There are 7 answers

0
jwernerny On BEST ANSWER

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.

0
Steve On

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".

0
hecvd On

Answer just for reference:

I was including

#include myclass.cpp

instead of

#include myclass.h
0
user1408605 On

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.

1
Zciurus On

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

0
Assimilater On

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.

0
iammilind On

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.