问题描述
我在头文件中定义一个结构,然后在相应的 .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 未命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!