【发布时间】:2012-12-23 12:26:42
【问题描述】:
我是 Qt 编程的初学者,并且在我的编程中使用代码块。我创建了 3 个文件communicate.h、commmunicate.cpp 和 main.cpp,如下:
communicate.h
#ifndef COMMUNICATE_H
#define COMMUNICATE_H
#include <QWidget>
#include <QApplication>
#include <QPushButton>
#include <QLabel>
class Communicate : public QWidget
{
Q_OBJECT
public:
Communicate(QWidget *parent = 0);
private slots:
void OnPlus();
void OnMinus();
private:
QLabel *label;
};
#endif
communicate.cpp
#include "communicate.h"
Communicate::Communicate(QWidget *parent)
: QWidget(parent)
{
QPushButton *plus = new QPushButton("+", this);
plus->setGeometry(50, 40, 75, 30);
QPushButton *minus = new QPushButton("-", this);
minus->setGeometry(50, 100, 75, 30);
label = new QLabel("0", this);
label->setGeometry(190, 80, 20, 30);
connect(plus, SIGNAL(clicked()), this, SLOT(OnPlus()));
connect(minus, SIGNAL(clicked()), this, SLOT(OnMinus()));
}
void Communicate::OnPlus()
{
int val = label->text().toInt();
val++;
label->setText(QString::number(val));
}
void Communicate::OnMinus()
{
int val = label->text().toInt();
val--;
label->setText(QString::number(val));
}
main.cpp
#include "communicate.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Communicate window;
window.setWindowTitle("Communicate");
window.show();
return app.exec();
}
及其显示错误如下:
obj\Debug\main.o(.text$_ZN11CommunicateD1Ev[Communicate::~Communicate()]+0xb)||In function `ZN7QStringC1EPKc':|
C:\Qt\4.4.3\include\QtCore\..\..\src\corelib\arch\qatomic_windows.h||undefined reference to `vtable for Communicate'|
obj\Debug\main.o(.text$_ZN11CommunicateD1Ev[Communicate::~Communicate()]+0x17):C:\Qt\4.4.3\include\QtCore\..\..\src\corelib\arch\qatomic_windows.h||undefined reference to `vtable for Communicate'|
obj\Debug\communicate.o(.text+0x172)||In function `ZN11CommunicateC2EP7QWidget':|
E:\Project\cam2\communicate.cpp|5|undefined reference to `vtable for Communicate'|
obj\Debug\communicate.o(.text+0x17e):E:\Project\cam2\communicate.cpp|5|undefined reference to `vtable for Communicate'|
obj\Debug\communicate.o(.text+0x63a)||In function `ZN11CommunicateC1EP7QWidget':|
E:\Project\cam2\communicate.cpp|5|undefined reference to `vtable for Communicate'|
obj\Debug\communicate.o(.text+0x646):E:\Project\cam2\communicate.cpp|5|more undefined references to `vtable for Communicate' follow|
||=== Build finished: 6 errors, 0 warnings ===|
请各位大侠帮忙...想不通...
【问题讨论】:
-
确保您的项目文件(.pro)在 headers 部分包含communication.h 文件
-
这是重复的,不是太本地化
-
我找到的最佳答案,这里没有提到的是重新运行 qmake:stackoverflow.com/a/3650758/258418
-
感谢 ted,您重新运行的 qmake 评论为我解决了问题。不确定何时或如何触发问题,但最终解决的方法是手动删除 Makefile 文件,触发它在 Qt Creator 中重新生成。
-
@Jijo Jose 和大家:我不确定我的系统是否出了问题,但对我来说 删除构建目录 解决了这个问题。 (即使“全部清洁”也不够)。我猜这是因为
moc(生成的)文件无法访问标题。我希望这会对某人有所帮助。
标签: c++ user-interface qt4 codeblocks