为什么我在带有QT的VS中收到这些错误C2134,C4430等

Why do I get these errors C2134, C4430 etc, in VS with QT?

本文关键字:错误 C2134 C4430 QT VS 为什么      更新时间:2023-10-16

我有一个任务,在Visual Studio中使用QT插件做一个OpenGL应用程序。

我正在尝试编译代码,但遇到了一些错误。当我将我想在其中显示OpenGL内容的WIDGET提升到我创建的类时,就会发生这种情况。

以下是错误:

Error   error C2143: syntax error : missing ';' before '*'  32  1   QTOpenGLVS
Error   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   32  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   48  1   QTOpenGLVS
Error   error C2061: syntax error : identifier 'QTOpenGLVS' 48  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   49  1   QTOpenGLVS
Error   error C2227: left of '->setObjectName' must point to class/struct/union/generic type    49  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   53  1   QTOpenGLVS
Error   error C2227: left of '->sizePolicy' must point to class/struct/union/generic type   53  1   QTOpenGLVS
Error   error C2228: left of '.hasHeightForWidth' must have class/struct/union  53  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   54  1   QTOpenGLVS
Error   error C2227: left of '->setSizePolicy' must point to class/struct/union/generic type    54  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   56  1   QTOpenGLVS
Error   error C2143: syntax error : missing ';' before '*'  32  1   QTOpenGLVS
Error   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   32  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   48  1   QTOpenGLVS
Error   error C2061: syntax error : identifier 'QTOpenGLVS' 48  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   49  1   QTOpenGLVS
Error   error C2227: left of '->setObjectName' must point to class/struct/union/generic type    49  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   53  1   QTOpenGLVS
Error   error C2227: left of '->sizePolicy' must point to class/struct/union/generic type   53  1   QTOpenGLVS
Error   error C2228: left of '.hasHeightForWidth' must have class/struct/union  53  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   54  1   QTOpenGLVS
Error   error C2227: left of '->setSizePolicy' must point to class/struct/union/generic type    54  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   56  1   QTOpenGLVS
Error   error C2143: syntax error : missing ';' before '*'  32  1   QTOpenGLVS
Error   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   32  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   48  1   QTOpenGLVS
Error   error C2061: syntax error : identifier 'QTOpenGLVS' 48  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   49  1   QTOpenGLVS
Error   error C2227: left of '->setObjectName' must point to class/struct/union/generic type    49  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   53  1   QTOpenGLVS
Error   error C2227: left of '->sizePolicy' must point to class/struct/union/generic type   53  1   QTOpenGLVS
Error   error C2228: left of '.hasHeightForWidth' must have class/struct/union  53  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   54  1   QTOpenGLVS
Error   error C2227: left of '->setSizePolicy' must point to class/struct/union/generic type    54  1   QTOpenGLVS
Error   error C2065: 'widget' : undeclared identifier   56  1   QTOpenGLVS

我拥有的类是main.cpp,qtopenglvs.cpp和头文件:qtopenglvs.h

主.cpp:

#include "qtopenglvs.h"
#include <QtWidgets/QApplication>
#include "MyGlWindow.h"

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

Qtopenglvs.cpp

#include "qtopenglvs.h"
#include <GL/glut.h>
QTOpenGLVS::QTOpenGLVS(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}
void QTOpenGLVS::initiliazeGL(){
    glClearColor(1, 1, 0, 1);
}
void QTOpenGLVS::paintGL(){
}
void QTOpenGLVS::resizeGL(int w, int h){
}

Qtopenglvs.h

#ifndef QTOPENGLVS_H
#define QTOPENGLVS_H
#include <QtWidgets/QMainWindow>
#include "ui_qtopenglvs.h"
class QTOpenGLVS : public QMainWindow
{
    Q_OBJECT
public:
    explicit QTOpenGLVS(QWidget *parent = 0);
    void initiliazeGL();
    void paintGL();
    void resizeGL(int w, int h);
private:
    Ui::QTOpenGLVSClass ui;
};
#endif // QTOPENGLVS_H

编辑ui_qtopenglvs.h:

/*******************************************************************************    *
** Form generated from reading UI file 'qtopenglvs.ui'
**
** Created by: Qt User Interface Compiler version 5.5.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_QTOPENGLVS_H
#define UI_QTOPENGLVS_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
#include "qtopenglvs.h"
QT_BEGIN_NAMESPACE
class Ui_QTOpenGLVSClass
{
public:
    QWidget *centralWidget;
    QHBoxLayout *horizontalLayout;
    QTOpenGLVS *widget;
    QVBoxLayout *verticalLayout;
    QSpacerItem *verticalSpacer;
    QPushButton *pushButton;
void setupUi(QMainWindow *QTOpenGLVSClass)
{
    if (QTOpenGLVSClass->objectName().isEmpty())
        QTOpenGLVSClass->setObjectName(QStringLiteral("QTOpenGLVSClass"));
    QTOpenGLVSClass->resize(834, 552);
    centralWidget = new QWidget(QTOpenGLVSClass);
    centralWidget->setObjectName(QStringLiteral("centralWidget"));
    horizontalLayout = new QHBoxLayout(centralWidget);
    horizontalLayout->setSpacing(6);
    horizontalLayout->setContentsMargins(11, 11, 11, 11);
    horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
    widget = new QTOpenGLVS(centralWidget);
    widget->setObjectName(QStringLiteral("widget"));
    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(widget->sizePolicy().hasHeightForWidth());
    widget->setSizePolicy(sizePolicy);
    horizontalLayout->addWidget(widget);
    verticalLayout = new QVBoxLayout();
    verticalLayout->setSpacing(6);
    verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
    verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    verticalLayout->addItem(verticalSpacer);
    pushButton = new QPushButton(centralWidget);
    pushButton->setObjectName(QStringLiteral("pushButton"));
    verticalLayout->addWidget(pushButton);

    horizontalLayout->addLayout(verticalLayout);
    QTOpenGLVSClass->setCentralWidget(centralWidget);
    retranslateUi(QTOpenGLVSClass);
    QObject::connect(pushButton, SIGNAL(clicked()), QTOpenGLVSClass, SLOT(close()));
    QMetaObject::connectSlotsByName(QTOpenGLVSClass);
} // setupUi
void retranslateUi(QMainWindow *QTOpenGLVSClass)
{
    QTOpenGLVSClass->setWindowTitle(QApplication::translate("QTOpenGLVSClass", "QTOpenGLVS", 0));
    pushButton->setText(QApplication::translate("QTOpenGLVSClass", "Quit", 0));
} // retranslateUi
};
namespace Ui {
   class QTOpenGLVSClass: public Ui_QTOpenGLVSClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_QTOPENGLVS_H
缺少

QWidget的包含,因此在qtopenglvs.h中无法识别。

#include <QWidget>

qtopenglvs.h标头。

看起来好像您剥离了该问题的代码,因为 widget 可能存在另一个问题,此处不存在。

编辑:该代码包含一个循环依赖关系(包括 A.h 中的 B.h 和 B.h 中的 A.h(,这是行不通的。我不完全知道你想要实现什么,因为setupUi是从构造函数调用的,并创建了一个新的QTOpenGLVS(再次调用构造函数(,它将以无限递归结束。目前看来,每个QTOpenGLVS都应该有一个QTOpenGLVS