为什么我在带有QT的VS中收到这些错误C2134,C4430等
Why do I get these errors C2134, C4430 etc, in VS with QT?
我有一个任务,在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
?
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 为什么我在带有QT的VS中收到这些错误C2134,C4430等