这个问题类似于之前提出的问题,但是尽管定义了我的类和函数,但Qt仍然无法识别我的类和函数。我在Qt版本仅为5.5而Windows版本为5.11的linux中这样做。在Windows中运行相同的程序时,没有任何错误。
我收到“对QCanBus::instance()
的未定义引用”错误
每个QCanBus
函数都会发生这种情况。在我来自connectdialogue.cpp
的代码段中,仅出现第一个匹配项,并且可以在倒数第二个代码行找到它并给出错误:
“对QCanBus::instance()
的未定义引用”
“对QCanBus::plugins()
的未定义引用”
尽管在qcanbus.h
头文件中定义了这些错误,但仍给出了这些错误。
我尝试在static QCanBus *instance()
的QCanBus *instance()
行下添加#include
或connectdialogue.cpp
行,但是随后出现警告:
当在特色代码段的倒数第二行中明显使用“ QCanBus::instance()
,但未使用”时。
如何解决这些错误?
connectdialogue.cpp
#include "connectdialog.h"
#include "ui_connectdialog.h"
#include "qcanbus.h"
ConnectDialog::ConnectDialog(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::ConnectDialog)
{
m_ui->setupUi(this);
m_ui->errorFilterEdit->setValidator(new QIntValidator(0, 0x1FFFFFFFU, this));
m_ui->loopbackBox->addItem(tr("unspecified"), QVariant());
m_ui->loopbackBox->addItem(tr("false"), QVariant(false));
m_ui->loopbackBox->addItem(tr("true"), QVariant(true));
m_ui->receiveOwnBox->addItem(tr("unspecified"), QVariant());
m_ui->receiveOwnBox->addItem(tr("false"), QVariant(false));
m_ui->receiveOwnBox->addItem(tr("true"), QVariant(true));
m_ui->canFdBox->addItem(tr("false"), QVariant(false));
m_ui->canFdBox->addItem(tr("true"), QVariant(true));
m_ui->dataBitrateBox->setFlexibleDateRateEnabled(true);
connect(m_ui->okButton, &QPushButton::clicked, this, &ConnectDialog::ok);
connect(m_ui->cancelButton, &QPushButton::clicked, this, &ConnectDialog::cancel);
connect(m_ui->useConfigurationBox, &QCheckBox::clicked,
m_ui->configurationBox, &QGroupBox::setEnabled);
connect(m_ui->backendListBox, &QComboBox::currentTextChanged,
this, &ConnectDialog::backendChanged);
connect(m_ui->interfaceListBox, &QComboBox::currentTextChanged,
this, &ConnectDialog::interfaceChanged);
m_ui->rawFilterEdit->hide();
m_ui->rawFilterLabel->hide();
m_ui->backendListBox->addItems(QCanBus::instance()->plugins());
updateSettings();
}
qcanbus.h
#ifndef QCANBUS_H
#define QCANBUS_H
#include <QtCore/qobject.h>
#include "qserialbusglobal.h"
#include "qcanbusdevice.h"
#include "qcanbusdeviceinfo.h"
QT_BEGIN_NAMESPACE
class Q_SERIALBUS_EXPORT QCanBus : public QObject
{
Q_OBJECT
public:
static QCanBus *instance();
QStringList plugins() const;
QList<QCanBusDeviceInfo> availableDevices(const QString &plugin, QString *errorMessage = nullptr) const;
QCanBusDevice *createDevice(const QString &plugin,
const QString &interfaceName,
QString *errorMessage = nullptr) const;
private:
QCanBus(QObject *parent = nullptr);
Q_DISABLE_COPY(QCanBus)
};
QT_END_NAMESPACE
#endif // QSERIALBUS_H
最佳答案
QCanBus缺少QCanBus.cpp,未声明。