c++ struct does not name a type(c ++ struct 未命名类型)
问题描述
我在头文件中定义一个结构,然后在相应的 .cpp 文件中设置其成员.为此,我使用了一个函数,该函数应该在其范围内创建一个(相同的)结构,然后返回它.像这样:
I am defining a structure in a header file, and then setting its members in the corrosponding .cpp file. For doing this I am using a function that is supposed to create a (same) structure in its scope, and then return it. Something like this:
在标题中:
#include <things>
class GLWindow : public QGLWidget, public QGLFunctions
{
Q_OBJECT
public:
GLWindow(QWidget *parent = 0);
~GLWindow();
//....
struct Drawable
{
GLuint vertexBuffer;
GLuint indexBuffer;
int faceCount;
QMatrix4x4 transform;
}cube;
GLuint cubeTex;
Drawable CreateDrawable(GLfloat* C_vertices, GLfloat* C_tex, GLfloat* C_normals, GLushort* C_facedata, int faces);
//.....
};
在 cpp 文件中:
#include "glwindow.h"
Drawable GLWindow :: CreateDrawable(GLfloat *C_vertices, GLfloat *C_tex, GLfloat *C_normals, GLushort *C_facedata, int faces)
{
int faceCount =faces;
QMatrix4x4 Transform;
Transform.setToIdentity();
GLuint VB;
/*Create vertexbuffer...*/
GLuint IB;
/*Create indexbuffer...*/
Drawable drawable;
drawable.faceCount = fCount;
drawable.transform = Transform;
drawable.vertexBuffer = VB;
drawable.indexBuffer = IB;
return drawable;
}
void GLWindow :: someOtherFunction()
{
//.....
cube = CreateDrawable(cube_vertices, cube_tex, cube_normals, cube_facedata, cube_face);
//.....
}
我收到一个错误,指出 'Drawable' 没有命名类型,但我不明白为什么我会收到这个错误,或者我可以做些什么来消除它.>
I am getting an error stating that 'Drawable' does not name a type, but I can't comprehend why I am getting this error, or what I can do to eliminate it.
推荐答案
你需要在cpp文件中限定Drawable
:
You need to qualify Drawable
in the cpp file:
GLWindow::Drawable GLWindow :: CreateDrawable(GLfloat *C_vertices, GLfloat *C_tex, GLfloat *C_normals, GLushort *C_facedata, int faces)
在 cpp 文件中,在成员方法之外,您在类上下文之外进行操作.在方法内部你可以使用Drawable
,但在外部(包括返回类型),你需要使用GLWindow::Drawable
.
In the cpp file, outside the member methods, you're operating outside the class context. Inside the methods you can use Drawable
, but outside (including return type), you need to use GLWindow::Drawable
.
如果你实际上从方法中返回了一个 Drawable
,而不是一个 void
- 也是一个错误.
That's if you're actually returning a Drawable
from the method, not a void
- also an error.
这篇关于c ++ struct 未命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c ++ struct 未命名类型
基础教程推荐
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++,'if' 表达式中的变量声明 2021-01-01