【问题标题】:c++ struct does not name a typec ++结构没有命名类型
【发布时间】:2012-03-07 07:49:39
【问题描述】:

我在头文件中定义一个结构,然后在对应的 .cpp 文件中设置其成员。为此,我使用了一个函数,该函数应该在其范围内创建一个(相同的)结构,然后返回它。像这样的:

在标题中:

#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' 没有命名类型,但我无法理解为什么会收到此错误,或者我可以做些什么来消除它。 p>

【问题讨论】:

  • 可能是拼写错误 CreateDrawable 在您的标头声明中返回 void 并在源代码中返回 Drawable
  • 在你的头文件中,不应该是Drawable CreateDrawable(...)而不是void CreateDrawable(...)吗?
  • another.anon.coward 和 Treb 是的。已编辑。
  • @LuchianGrigore 是的,我打算这样做,但不是现在。感谢您提出。

标签: c++ qt


【解决方案1】:

您需要在 cpp 文件中限定Drawable

GLWindow::Drawable GLWindow :: CreateDrawable(GLfloat *C_vertices, GLfloat *C_tex, GLfloat *C_normals, GLushort *C_facedata, int faces)

在 cpp 文件中,在成员方法之外,您在类上下文之外进行操作。方法内部可以使用Drawable,但外部(包括返回类型)需要使用GLWindow::Drawable

如果您实际上是从该方法返回 Drawable,而不是 void - 也是一个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 2023-03-14
    • 2023-03-06
    相关资源
    最近更新 更多