C++ Qt 多重定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4964863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Qt Multiple Definitions
提问by John M.
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.
我正在尝试使用 MinGW 编译器在 Qt 中使用 C++ 创建一个简单的 GUI 应用程序(到目前为止)。然而,编译器,通知我有一个multiple definition of 'WiimoteScouter::WiimoteScouter(QWidget*)'
对line 4
的wiimotescouter.cpp
。我正在使用检查来确保标题没有多次包含在内,但显然它不起作用,我不知道为什么。
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();
}
回答by jwernerny
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.
当源文件在项目(.pro 或 .pri)文件中重复时,我曾见过这种情况。检查项目文件中的所有“SOURCES =”和“SOURCES + =”行,并确保 cpp 文件不在其中多次。
回答by Steve
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".
我不使用 MinGW 但这听起来像是链接器错误而不是编译器错误。如果是这种情况,那么您应该检查 .CPP 文件是否没有两次添加到项目中。我还注意到扩展名是“php”,这很不寻常,因为它应该是“cpp”。
回答by Assimilater
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.
如果您在不同文件夹中有两个同名的 .ui 文件,也会发生这种情况。它们对应的头文件建立在同一目录中,导致一个被覆盖。至少那是我的问题。
回答by user1408605
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.
剪切和粘贴解决了问题,下次我手动创建插槽时需要检查。
回答by iammilind
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. 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:
起初,在 .pro 文件中,我找不到任何类似的文件名。然后敏锐地观察我发现,我正在编译的外部 google protobuf 库在其文件夹中有一些库文件,名为:
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.
案例和扩展是不同的,但不知何故它在 Qt 编译中造成了混乱。我只是在这些库文件中添加了一个下划线,一切正常。
回答by hecvd
Answer just for reference:
回答仅供参考:
I was including
我包括
#include myclass.cpp
instead of
代替
#include myclass.h