简体   繁体   English

C++ Qt 多重定义

[英]C++ Qt Multiple Definitions

I'm trying to create a simple GUI application (so far) in Qt with C++ using the MinGW compiler.我正在尝试使用 MinGW 编译器在 Qt 中使用 C++ 创建一个简单的 GUI 应用程序(到目前为止)。 However, the compiler is informing me that I have a multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)' on line 4 of wiimotescouter.cpp .但是,编译器通知我,我在multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)' line 4multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)' 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:这是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"));
}

Lastly, the main.cpp:最后,main.cpp:

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

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

    WiimoteScouter wiimoteScouter;
    wiimoteScouter.show();

    return app.exec();
}

I've seen this happen before when a source file got duplicated in the project (.pro or .pri) file.当源文件在项目(.pro 或 .pri)文件中重复时,我曾见过这种情况。 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.检查项目文件中的所有“SOURCES =”和“SOURCES + =”行,并确保 cpp 文件不在其中多次。

I don't use MinGW but this sounds like a linker error rather than a compiler error.我不使用 MinGW 但这听起来像是链接器错误而不是编译器错误。 If this is the case then you should check that the .CPP file is not added to the project twice.如果是这种情况,那么您应该检查 .CPP 文件是否没有两次添加到项目中。 I also noticed that the extension is "php", this is very unusual as it should be "cpp".我还注意到扩展名是“php”,这很不寻常,因为它应该是“cpp”。

Answer just for reference:回答仅供参考:

I was including我包括

#include myclass.cpp

instead of而不是

#include myclass.h

This can also happen if you have two .ui files with the same name in different folders.如果您在不同文件夹中有两个同名的 .ui 文件,也会发生这种情况。 Their corresponding headers are built in the same directory, resulting in one being overwritten.它们对应的头文件建立在同一目录中,导致一个被覆盖。 At least that was my problem.至少那是我的问题。

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.剪切和粘贴解决了问题,下次我手动创建插槽时需要检查。

For me it was due to Qt's compilation model in Windows using MinGW.对我来说,这是由于 Qt 在 Windows 中使用 MinGW 的编译模型。

My code compiled perfectly fine for Linux, but for Windows the linker errors were happening for following files:我的代码为 Linux 编译得非常好,但对于 Windows,以下文件发生了链接器错误:

Message.cpp
Util.cpp

At first, in the .pro file, I couldn't find any similar file names.起初,在 .pro 文件中,我找不到任何类似的文件名。 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:然后敏锐地观察我发现,我正在编译的外部 google protobuf 库在其文件夹中有一些库文件,名为:

message.cc
util.cc

The cases and the extensions were different, but somehow it created mess in the Qt compilation.案例和扩展是不同的,但不知何故它在 Qt 编译中造成了混乱。 I just added an underscore to those library files and the things worked fine.我只是在这些库文件中添加了一个下划线,一切正常。

就我而言,这是由于在头文件中全局声明了该函数引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM