为什么我在带QT的VS中遇到C2134,C4430等错误?

时间:2015-11-06 11:55:03

标签: c++ visual-studio qt opengl

我有一个任务是在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

main.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

EDITED 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

1 个答案:

答案 0 :(得分:0)

QWidget的附加内容缺失,因此无法在qtopenglvs.h中识别。

添加

#include <QWidget>

qtopenglvs.h标题。

看起来您已经删除了问题的代码,因为widget可能存在另一个问题,此处不存在。

修改 该代码包含一个循环依赖(在A.h中包括B.h,在B.h中包括A.h),它不起作用。我不知道你想要实现什么,因为从构造函数调用setupUi并创建一个新的QTOpenGLVS(它再次调用构造函数),它将以无限递归结束。目前看起来好像每个QTOpenGLVS应该有QTOpenGLVS作为孩子?