未在此作用域中声明的信号/对class::方法的未定义引用

Signal not declared in this scope/undefined reference to class::method

本文关键字:class 方法 引用 未定义 信号 作用域 声明      更新时间:2023-10-16

我一直试图在我的Qt应用程序中整理自定义信号,我似乎无法让它编译。基本上,我有一个带有插槽的窗口来处理按钮按下(slotCalibrate()),另一个插槽用于接收消息并通过QTextEdit (slotMessage())将其输出到屏幕。这两个函数在单独的情况下可以很好地工作。

这里是windows .cpp, slotCalibrate()目前正在向Output类发送预制数据。在最终的设计中,slotCalibrate()将激活一些硬件上的校准过程,其结果将是发送到Output.cpp

的数据。
#include "window.h"
#include "output.h"
#include <QPushButton>
#include <QTextEdit>
#include <QApplication>
#include <string>
using namespace std;
Window::Window(QWidget *parent) :
    QWidget(parent)
{
    setFixedSize(640, 480);
    calButton = new QPushButton("Calibrate", this);
    calButton->setGeometry(280, 20, 80, 30);
    calButton->setToolTip("Press to zero connected transducers.");
    connect(calButton, SIGNAL(clicked()), this, SLOT(slotCalibrate()));
    quitButton = new QPushButton("Quit", this);
    quitButton->setGeometry(550, 440, 80, 30);
    connect(quitButton, SIGNAL(clicked()), QApplication::instance(), SLOT(quit()));
    readout = new QTextEdit(this);
    readout->setReadOnly(1);
    readout->setPlainText("Testing, testing, 1, 2, 3.n");
    readout->setGeometry(10, 70, 620, 360);
}
void Window::slotCalibrate()
{
    string sn[5] = {"1234", "2463", "7821", "5027", "5981"};
    Output *op = new Output();
    op->writeMessage(1, 5, sn);
    //I also tried the following with exactly the same result
    //Output op;
    //op.writeMessage(1, 5, sn);
}
void Window::slotMessage(string message)
{
    QString qstr = QString::fromStdString(message); //Convert from string to QString
    readout->append(qstr);                          //Print message to screen
}

我试图让按钮按下调用output.cpp的构造函数,然后调用函数writeMessage来构造并在window.cpp

中向slotMessage发送消息
#include "output.h"
#include <string>
#include <sstream>
using namespace std;
Output::Output(QWidget *parent) :
    QWidget(parent)
{
    connect (this, SIGNAL(sendMessage(string)), parentWidget(), SLOT(slotMessage(string)));
}
//void sendMessage(string)
//{
//}
void writeMessage(int cat, int count, string sn[])
{
    int i;
    stringstream ss;
    switch (cat)
    {
        case 1 : //Case where calibration of one or more nodes was successful
            ss << count << " transducers were successfully calibrated.n Their serial numbers are:";
            for (i=0; i<cat; i++)
            {
                ss << "n" << sn[i];
            }
            break;
        case 2:  //Case where calibration of one or more nodes failed
            ss << "One or more transducers failed to calibrate.";
            break;
        case 3:  //Case where no config file is found
            ss << "WARNING! Could not find 'params.config'. The default values will be used for calibration.";
                  break;
    }
    emit sendMessage(ss.str());
}

不幸的是,对于这样的代码,编译器会对我大喊大叫。它说:'sendMessage' was not declared in this scope

我已经在头文件中将sendMessage声明为信号,并且我的印象是信号不需要在代码中实现。

然而,我决定尝试实现一个名为sendMessage的空函数。这消除了编译器错误,但引入了另一个错误。在window.cpp中调用op.writeMessage()时,我得到错误:"对' Output::writeMessage(int, int, std::string*)'的未定义引用"

我也尝试在Output构造函数中调用writeMessage,并且我得到相同的错误。

我完全迷路了,并且已经在这个问题上工作了几天了,所以任何帮助都会非常感激。

为了完整起见,这里分别是头文件window.h和output.h:

window.h

#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <string>
class QPushButton;
class QTextEdit;
class Window : public QWidget
{
    Q_OBJECT
public:
    explicit Window(QWidget *parent = 0);
    QTextEdit *readout;
private:
    QPushButton *calButton;
    QPushButton *quitButton;
signals:
public slots:
    void slotCalibrate();
private slots:
    void slotMessage(std::string);
};
#endif // WINDOW_H

output.h

#ifndef OUTPUT_H
#define OUTPUT_H
#include <QWidget>
#include <QObject>
#include <string>
class Output : public QWidget
{
    Q_OBJECT
public:
    explicit Output(QWidget *parent = 0);
    void writeMessage(int, int, std::string[]);
signals:
    void sendMessage(std::string);
public slots:
};
#endif // OUTPUT_H

当然这行不通。您定义了writeMessage()函数,但将其定义为全局函数。你必须在定义前加上一个"Output::"