C++ Qt 多重定义

发布于 2024-10-16 22:02:22 字数 1289 浏览 2 评论 0原文

我正在尝试使用 MinGW 编译器在 Qt 中使用 C++ 创建一个简单的 GUI 应用程序(到目前为止)。但是,编译器通知我,我在 wiimotescouter.cpp第 4 行上有一个 'WiimoteScouter::WiimoteScouter(QWidget*)' 的多个定义代码>.我正在使用检查来确保未多次包含标头,但显然它不起作用,我不确定为什么。

这是头文件:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

星星的轨迹 2024-10-23 22:02:22

我以前见过当源文件在项目(.pro 或 .pri)文件中重复时发生这种情况。检查项目文件中的所有“SOURCES =”和“SOURCES +=”行,并确保 cpp 文件不在其中多次。

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.

傾旎 2024-10-23 22:02:22

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

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

挥剑断情 2024-10-23 22:02:22

答案仅供参考:

我包括

#include myclass.cpp

而不是

#include myclass.h

Answer just for reference:

I was including

#include myclass.cpp

instead of

#include myclass.h
呆萌少年 2024-10-23 22:02:22

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

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.

烙印 2024-10-23 22:02:22

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

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

痴情换悲伤 2024-10-23 22:02:22

我是 Qt 的新手,我的情况是我在信号中提供了代码逻辑,而 MOC 期望信号声明是简单的函数原型,大括号内没有任何实现或代码。

此外,与信号相关的任何代码逻辑或操作都应该在连接到信号的槽中或在类的其他成员函数中实现。

I am a newbie to Qt and my case was that I provided code logic within my signal, whereas the MOC expects signal declarations to be simple function prototypes, without any implementation or code within the curly braces.

Further, any code logic or actions related to the signal should be implemented in the slots that are connected to the signal, or in other member functions of the class.

遗忘曾经 2024-10-23 22:02:22

当我在头文件中的信号标题下列出插槽声明(而不是插槽声明)时,我收到此错误消息。对于遇到此错误消息的人来说,还有另一件事需要检查。

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

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.

厌味 2024-10-23 22:02:22

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

我的代码在 Linux 上编译得很好,但在 Windows 上,以下文件发生了链接器错误:

Message.cpp
Util.cpp

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

message.cc
util.cc

案例和扩展名不同,但不知何故它在 Qt 编译中造成了混乱。我只是在这些库文件中添加了一个下划线,一切工作正常。

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.

东北女汉子 2024-10-23 22:02:22

我正在为同样的错误而苦苦挣扎。就我而言,错误在于我在“信号”下声明了该方法,而必须在“槽”下声明它。

I was struggling with the same error. In my case the mistake was that I was declaring the method under 'signals', while it had to be declared under 'slots'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文