在声明类类型的QVector时出错

Error while declaring QVector of class type

本文关键字:QVector 出错 类型 声明      更新时间:2023-10-16

在qt中声明类类型的qVector时获取错误。

错误:"不允许使用不完整的类型"

我不明白是什么原因导致此错误。

如果我在main.cpp中#include" storage.h"并声明了类storeage的QVECTOR,则可以正常工作,但是当我在波形类中进行相同的操作时,它会报告一个错误。

我已经尝试在波形类中向前声明存储类,但仍会遇到相同的错误。

任何帮助??

main.cpp

#include "test_subject_02.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TEST_SUBJECT_02 w;
    w.show();
    return a.exec();
}

testrongubject_02.h

#ifndef TEST_SUBJECT_02_H
#define TEST_SUBJECT_02_H
#include <QtGui/QMainWindow>
#include "ui_test_subject_02.h"
#include"waveform.h"
#include "storage.h"

class TEST_SUBJECT_02 : public QMainWindow
{
    Q_OBJECT
public:
    TEST_SUBJECT_02(QWidget *parent = 0, Qt::WFlags flags = 0);
    waveform *wv;
    ~TEST_SUBJECT_02();
private:
    Ui::TEST_SUBJECT_02Class ui;
};
#endif // TEST_SUBJECT_02_H

testrongubject_02.cpp

#include "test_subject_02.h"
TEST_SUBJECT_02::TEST_SUBJECT_02(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    QVector<storage> ser; //works fine here
    wv->readfile("e:/testing2/result/3/abc.cur");
}
TEST_SUBJECT_02::~TEST_SUBJECT_02()
{
}

waveform.h

#ifndef WAVEFORM_H
#define WAVEFORM_H
#include "storage.h"
#include <QObject>
class waveform : public QObject
{
    Q_OBJECT

public:
    waveform(QObject *parent=0);
    void readfile(QString);
    QVector <storage> myvector ; // incomplete type is not allowed
    ~waveform();
private:
};
#endif // WAVEFORM_H

waveform.cpp

#include "waveform.h"
waveform::waveform(QObject *parent)
    : QObject(parent)
{
}
void waveform::readfile(QString file)
{  
    QVector<storage> sw; //again error incomplete type is not allowed
}
waveform::~waveform()
{
}

存储.h

#ifndef STORAGE_H
#define STORAGE_H
#include <QObject>
class storage : public QObject
{
    Q_OBJECT
public:
    storage(QObject *parent);
    storage();
    storage(QString,QString);
    ~storage();
private:
    QString x;
    QString y;
};
#endif // STORAGE_H

storage.cpp

#include "storage.h"
storage::storage(QObject *parent)
    : QObject(parent)
{
}
storage::storage(QString xcord,QString ycord)
{
    x=xcord;
    y=ycord;
}
storage::storage()
{
}
storage::~storage()
{
}

您需要在必要的文件中明确包含 QVector(我相信waveform.h),因为QT使用了大量的正向声明,而它们在IDE中显示为正确的类,但它们不会编译,如果未正确定义在文件中。