問題描述
我在頭文件中定義一個結構,然后在相應的 .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 未命名類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!