Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 797
John M. Avatar asked Feb 11 '11 01:02

John M.


4 Answers

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

like image 34
Steve Avatar answered Nov 16 '22 03:11

Steve


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.

like image 57
jwernerny Avatar answered Nov 16 '22 04:11

jwernerny


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.

like image 1
Assimilater Avatar answered Nov 16 '22 03:11

Assimilater


Answer just for reference:

I was including

#include myclass.cpp

instead of

#include myclass.h
like image 1
hecvd Avatar answered Nov 16 '22 04:11

hecvd