本文介绍了C ++结构未命名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在头文件中定义一个结构,然后在相应的.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 而不是无效-也是一个错误。

That's if you're actually returning a Drawable from the method, not a void - also an error.

这篇关于C ++结构未命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:39
查看更多