【问题标题】:undefined reference to function send对函数发送的未定义引用
【发布时间】:2013-11-06 13:07:49
【问题描述】:

我是 C++ /Qt 编程的初学者。我制作了这个简单的对话框来检查 QLineEdit,如果输入的文本是“bob”,则应该启用 OK 按钮。 我无法让它编译成功,它给了我:

dialog.cpp|31|undefined reference to `Dialogmio::send()'

我做错了什么?

这是 dialog.h:

//dialog.h
#ifndef DIALOG_H_INCLUDED
#define DIALOG_H_INCLUDED
#include <QDialog>

class QPushButton;
class QLineEdit;

class Dialogmio : public QWidget


{


public:
Dialogmio(QWidget *parent =0);

signals:
void send ();

public slots:
void recip(QString &text);


private:

QLineEdit *linedit;
QPushButton *buttonOK;


};

#endif

这是 dialog.cpp:

//dialog.cpp
#include <QtGui>

#include "dialog.h"

Dialogmio::Dialogmio(QWidget *parent)
: QWidget(parent)
{

linedit = new QLineEdit();
buttonOK = new QPushButton("OK");
buttonOK->setEnabled(FALSE);


connect( linedit, SIGNAL( textChanged(const QString &) ), this, SLOT( recip(const QString &) ));
connect (this,SIGNAL( send()), this, SLOT( buttonOK->setEnabled(true)) );

QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(linedit);
layout->addWidget(buttonOK);
setLayout(layout);


}

void Dialogmio::recip(QString &text)
{
QString a = linedit->text();
if (a == "bob"){

emit send();   //here it gives me the error

}
}

这是 main.cpp:

#include <QApplication>
#include "dialog.h"


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

Dialogmio *dialog = new Dialogmio;
dialog->show();

return app.exec();
}

我按照建议插入了 Q_OBJECT 宏,现在我在第 7 行又遇到了一个错误:

dialog.cpp|7|undefined reference to `vtable for Dialogmio'|

【问题讨论】:

  • class Dialogmio: ... { 之后添加Q_OBJECT。您必须将它添加到您编写的从 QObject 扩展的任何类中,以使用信号/插槽机制。见thisthis
  • 嗨,对不起,我忘了提到我使用 Codeblocks 作为 IDE
  • 这很可能是Qt 问题。它应该与您使用的 IDE 无关。
  • 我现在又遇到一个错误...关于 vtable
  • connect (this,SIGNAL( send()), this, SLOT( buttonOK-&gt;setEnabled(true)) ); 无效

标签: c++ qt


【解决方案1】:

您首先包含 QDialog 的 Qt 文件,然后继续从 QWidget 继承。虽然从 QWidget 继承不是问题,但您是否打算实际从 QDialog(?) 继承,在这种情况下,您应该这样定义您的类:-

class Dialogmio : public QDialog
{
    Q_OBJECT

    public:
        Dialog(QWidget* parent);

    private slots:
        void aSlotFunction();
}

信号和槽机制是 Qt 独有的 C++ 扩展,为了让类使用它,该类必须包含 Q_OBJECT 宏,它添加了所有必要的功能。在构建阶段,Qt 解析头文件并创建扩展所需的代码,包括运行时类型信息、动态属性系统,当然还有信号和插槽。

正如您所说,您使用代码块作为 IDE,如果它在构建之前没有自动运行 qmake,那么每当您将任何信号或插​​槽添加到类以用于 moc(元-object-compiler) 来查看它们。

还有一点就是调用connect信号和槽是错误的:-

connect (this,SIGNAL( send()), this, SLOT( buttonOK->setEnabled(true)) );

SLOT宏中的参数带一个槽函数,所以需要创建一个槽并连接到发送信号:-

connect(this, SIGNAL(send()), this, SLOT(aSlotFunction());

在 aSlotFunction 中,您可以调用为按钮启用的集合:-

void Dialogmio::aSlotFunction()
{
    buttonOK->setEnabled(true);
}

如果您使用的是 Qt 5,则处理连接的语法更简单:-

connect(this, &Dialogmio::send, this, &Dialogmio::aSlotFunction);

由于此语法接受指向将要调用的函数的指针,因此它们实际上不必声明为插槽即可工作。此外,您不提供参数,因此如果它们发生变化,您也不必更新连接调用。

【讨论】:

  • 谢谢。我更正了代码,添加了 Q_OBJECT 宏,运行 qmake -project 和 qmake,仍然得到相同的错误 dialog.cpp|7|undefined reference to vtable for Dialogmio'| and|undefined reference to Dialogmio::send()'
  • 清理项目,确保删除生成的moc和目标文件。然后运行 ​​qmake 并构建您的项目。检查是否生成了新的 moc 文件。
  • 非常感谢大家!现在它工作正常。代码块也没有正确配置
  • @basicprogram,如果您有兴趣,有一个名为QtCreatorQt IDE 可以帮助您处理qmake
猜你喜欢
  • 2019-09-05
  • 2011-09-19
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 2012-10-07
相关资源
最近更新 更多