首页 文章

C Qt多重定义

提问于
浏览
10

我正在尝试使用MinGW编译器在Qt中使用C创建一个简单的GUI应用程序(到目前为止) . 但是,编译器通知我 multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)'line 4 wiimotescouter.cpp 上 . 我正在使用一个检查来确保 Headers 不是't included multiple times, but apparently it' s不起作用,我不知道为什么 .

这是头文件:

#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

这是cpp文件:

#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"));
}

最后,main.cpp:

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

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

    WiimoteScouter wiimoteScouter;
    wiimoteScouter.show();

    return app.exec();
}

6 回答

  • 0

    我已经看到过这种情况发生在源文件在项目(.pro或.pri)文件中重复之前 . 检查项目文件中的所有“SOURCES =”和“SOURCES =”行,并确保cpp文件不在那里多次 .

  • 0

    我不使用MinGW,但这听起来像链接器错误而不是编译器错误 . 如果是这种情况,那么您应该检查.CPP文件是否未添加到项目两次 . 我也注意到扩展名是“php”,这是非常不寻常的,因为它应该是“cpp” .

  • 43

    如果在不同文件夹中有两个同名的.ui文件,也会发生这种情况 . 它们相应的标头构建在同一目录中,导致一个被覆盖 . 至少这是我的问题 .

  • 1

    答案仅供参考:

    我包括在内

    #include myclass.cpp
    

    代替

    #include myclass.h
    
  • 2

    当我在插件文件中的信号 Headers 下列出我的插槽声明而不是插槽1时,我收到此错误消息 . 遇到此错误消息的任何人都要检查的另一件事 .

    剪切和粘贴解决了问题,需要在下次手动创建插槽时进行检查 .

  • 0

    对我来说,这是由于Qt在Windows中使用MinGW的编译模型 .

    我的代码编译完全适用于Linux,但对于Windows,链接器错误发生在以下文件中:

    Message.cpp
    Util.cpp
    

    首先,在.pro文件中,我找不到任何类似的文件名 . 然后敏锐地观察我发现,我正在编译的外部google protobuf库在其文件夹中有一些库文件命名为:

    message.cc
    util.cc
    

    案例和扩展是不同的,但不知何故,它在Qt编译中造成了混乱 . 我刚刚为这些库文件添加了下划线,并且工作正常 .

相关问题