Qt:对客户构造函数的未定义引用 class
Qt: undefined reference on constructor of customer class
我正在使用继承自 QAbstractTableModel
的自定义 class、GpibDevicesModel
,编译器提示构造函数中存在未定义的引用。但是我不明白为什么。
代码如下:
MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
//...
private:
GpibDevicesModel *m_gpibDevicesModel;
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//...
//undefined reference is located here
m_gpibDevicesModel = new GpibDevicesModel;
//...
}
GpibDevicesModel.h
#ifndef GPIBDEVICESMODEL_H
#define GPIBDEVICESMODEL_H
#include <QAbstractTableModel>
class MeasureDevice;
class GpibDevicesModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Columns{
NameCol = 0,
AddressCol,
IdCol,
TypeCol,
ConnectedCol,
EndCol //Should always be at end
};
//constructor and destructor
explicit GpibDevicesModel(QObject *parent = 0);
~GpibDevicesModel();
protected:
//Reimplemented from QAbstractTableModel
QVariant data(const QModelIndex &index, int role) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
//Data list getter and setter
QList<MeasureDevice *> measureDevices() const;
void setMeasureDevices(const QList<MeasureDevice *> &measureDevices);
private:
QList<MeasureDevice *> m_measureDevices;
};
#endif // GPIBDEVICESMODEL_H
GpibDevicesModel.cpp
#include "gpibdevicesmodel.h"
#include "measuredevice.h"
#include <QIcon>
GpibDevicesModel::GpibDevicesModel(QObject *parent)
: QAbstractTableModel(parent)
{}
GpibDevicesModel::~GpibDevicesModel()
{}
QVariant GpibDevicesModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid() || index.row() >= m_measureDevices.size())
return QVariant();
switch(role){
//This is roles for ComboBox
case Qt::DisplayRole:
switch(index.column()){
case NameCol: return m_measureDevices.at(index.row())->displayName();
case AddressCol:return m_measureDevices.at(index.row())->gpibName();
case IdCol: return m_measureDevices.at(index.row())->property("internal_id").toString();
case TypeCol: return m_measureDevices.at(index.row())->typeString();
}
break;
case Qt::DecorationRole:
switch(index.column()){
case ConnectedRole: {
MeasureDevice *md = m_measureDevices.at(index.row());
if(md->isConnected()) return QIcon(":/Icons/green_led.png");
else return QIcon(":/Icons/red_led.png");
}
}
break;
}
return QVariant();
}
int GpibDevicesModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_measureDevices.size();
}
int GpibDevicesModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return EndCol;
}
QVariant GpibDevicesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
static QStringList headers = QStringList() << tr("Name") << tr("Address") << tr("Id")
<< tr("Type") << tr("Connected") ;
if(role == Qt::DisplayRole && orientation == Qt::Vertical)
return QVariant(); //return section; //uncomment here to displayed row number
if(section >= headers.size())
return QVariant();
switch(role){
case Qt::DisplayRole: return headers.at(section);
}
return QVariant();
}
QList<MeasureDevice *> GpibDevicesModel::measureDevices() const
{
return m_measureDevices;
}
void GpibDevicesModel::setMeasureDevices(const QList<MeasureDevice *> &measureDevices)
{
beginResetModel();
m_measureDevices = measureDevices;
endResetModel();
}
这里是编译器的错误信息
mainwindow.cpp:1430: undefined reference to GpibDevicesModel::GpibDevicesModel(QObject*)'
我认为这将是一件愚蠢的事情,一如既往...但我无法弄清楚这个错误。我以同样的方式使用另一个自定义模型,我没有任何问题。
如我所料,这很愚蠢...
我通过手动删除构建文件夹和 Makefile(makefile,makefile.debug 和 makefile.release)解决了这个问题,然后我再次 运行 qmake -> build 并且没问题。所以这不是代码的问题,也许 makefile 出于未知原因处于只读状态。
感谢 Frogatto 和 Frank Osterfeld 的帮助以及抽出宝贵时间。
我正在使用继承自 QAbstractTableModel
的自定义 class、GpibDevicesModel
,编译器提示构造函数中存在未定义的引用。但是我不明白为什么。
代码如下:
MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
//...
private:
GpibDevicesModel *m_gpibDevicesModel;
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//...
//undefined reference is located here
m_gpibDevicesModel = new GpibDevicesModel;
//...
}
GpibDevicesModel.h
#ifndef GPIBDEVICESMODEL_H
#define GPIBDEVICESMODEL_H
#include <QAbstractTableModel>
class MeasureDevice;
class GpibDevicesModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Columns{
NameCol = 0,
AddressCol,
IdCol,
TypeCol,
ConnectedCol,
EndCol //Should always be at end
};
//constructor and destructor
explicit GpibDevicesModel(QObject *parent = 0);
~GpibDevicesModel();
protected:
//Reimplemented from QAbstractTableModel
QVariant data(const QModelIndex &index, int role) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
//Data list getter and setter
QList<MeasureDevice *> measureDevices() const;
void setMeasureDevices(const QList<MeasureDevice *> &measureDevices);
private:
QList<MeasureDevice *> m_measureDevices;
};
#endif // GPIBDEVICESMODEL_H
GpibDevicesModel.cpp
#include "gpibdevicesmodel.h"
#include "measuredevice.h"
#include <QIcon>
GpibDevicesModel::GpibDevicesModel(QObject *parent)
: QAbstractTableModel(parent)
{}
GpibDevicesModel::~GpibDevicesModel()
{}
QVariant GpibDevicesModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid() || index.row() >= m_measureDevices.size())
return QVariant();
switch(role){
//This is roles for ComboBox
case Qt::DisplayRole:
switch(index.column()){
case NameCol: return m_measureDevices.at(index.row())->displayName();
case AddressCol:return m_measureDevices.at(index.row())->gpibName();
case IdCol: return m_measureDevices.at(index.row())->property("internal_id").toString();
case TypeCol: return m_measureDevices.at(index.row())->typeString();
}
break;
case Qt::DecorationRole:
switch(index.column()){
case ConnectedRole: {
MeasureDevice *md = m_measureDevices.at(index.row());
if(md->isConnected()) return QIcon(":/Icons/green_led.png");
else return QIcon(":/Icons/red_led.png");
}
}
break;
}
return QVariant();
}
int GpibDevicesModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_measureDevices.size();
}
int GpibDevicesModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return EndCol;
}
QVariant GpibDevicesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
static QStringList headers = QStringList() << tr("Name") << tr("Address") << tr("Id")
<< tr("Type") << tr("Connected") ;
if(role == Qt::DisplayRole && orientation == Qt::Vertical)
return QVariant(); //return section; //uncomment here to displayed row number
if(section >= headers.size())
return QVariant();
switch(role){
case Qt::DisplayRole: return headers.at(section);
}
return QVariant();
}
QList<MeasureDevice *> GpibDevicesModel::measureDevices() const
{
return m_measureDevices;
}
void GpibDevicesModel::setMeasureDevices(const QList<MeasureDevice *> &measureDevices)
{
beginResetModel();
m_measureDevices = measureDevices;
endResetModel();
}
这里是编译器的错误信息
mainwindow.cpp:1430: undefined reference to GpibDevicesModel::GpibDevicesModel(QObject*)'
我认为这将是一件愚蠢的事情,一如既往...但我无法弄清楚这个错误。我以同样的方式使用另一个自定义模型,我没有任何问题。
如我所料,这很愚蠢... 我通过手动删除构建文件夹和 Makefile(makefile,makefile.debug 和 makefile.release)解决了这个问题,然后我再次 运行 qmake -> build 并且没问题。所以这不是代码的问题,也许 makefile 出于未知原因处于只读状态。
感谢 Frogatto 和 Frank Osterfeld 的帮助以及抽出宝贵时间。