“不允许不完整类型”错误

时间:2016-07-05 09:54:26

标签: c++ class c++11 opengl

“sizeof(points)”是抛出错误的部分(标记如下)。我不知道发生了什么/出了什么问题。我是OpenGL的新手,我正在试验我学到的知识,因此可以绘制多个三角形。我还将代码放在pastebin here

VertexObject.h

#pragma once

#include <stdio.h>
#include <stdlib.h>

#include <GL\glew.h>
#include <GLFW\glfw3.h>

class VertexObject
{

public:
    VertexObject ( );

    void SetArray ( GLfloat Points [] );

    void SetBuffer ( GLuint* VBO );

    GLfloat Points [ ] = {
         1.0f , 0.0f , 1.0f,
         0.0f , 1.0f , 1.0f,
        -1.0f , 0.0f , 1.0f
    };

private:



};

VertexObject.cpp

#include "VertexObject.h"

#include <stdio.h>
#include <stdlib.h>

#include <GL\glew.h>
#include <GLFW\glfw3.h>

void VertexObject::SetArray ( GLfloat Points [ ] )
{

    //Generate Vertex Array Object
    GLuint vaoID1;
    //Generates an array for the VAO
    glGenVertexArrays ( 1 , &vaoID1 );
    //Assigns the array to the Vertex Array Object
    glBindVertexArray ( vaoID1 );

    //Fills in the array

    for ( int i = 0; i < sizeof ( Points ); i++ ) //Error occurs here
    {

        this->Points [ i ] = Points [ i ];

    }

}

void VertexObject::SetBuffer ( GLuint* VBO )
{

    //Generate Vertex Buffer Object
    glGenBuffers ( 1 , VBO );
    glBindBuffer ( GL_ARRAY_BUFFER , *VBO );
    glBufferData ( GL_ARRAY_BUFFER ,sizeof(Points) , Points , GL_STATIC_DRAW );

}

1 个答案:

答案 0 :(得分:0)

正如PcAF所说,sizeof(Points)给出了指针的大小,而不是数组Points中元素的数量。

您可以认为可以将sizeof(Points)替换为sizeof(this->Points),但还有另一个问题:sizeof(this->Points)请不要给你9(元素数量)但{{1} }}

所以你应该使用9 * sizeof(GLfloat)

但我认为更大的问题是

sizeof(Points)/sizeof(Points[0])

不是有效的C ++ 11(C ++ 11而不是C ++ 98,因为在非静态数据成员的类初始化中是C ++ 11特性)因为在类数组中你​​必须明确大小,所以

GLfloat Points [ ] = {
     1.0f , 0.0f , 1.0f,
     0.0f , 1.0f , 1.0f,
    -1.0f , 0.0f , 1.0f
};

但是如果你必须明确大小,你可以在静态成员中记住它,比如

GLfloat Points [ 9 ] = {
     1.0f , 0.0f , 1.0f,
     0.0f , 1.0f , 1.0f,
    -1.0f , 0.0f , 1.0f
};

以这种方式在循环中使用

static const std::size_t  pointsSize = 9;
GLfloat Points [ pointsSize ] = {
     1.0f , 0.0f , 1.0f,
     0.0f , 1.0f , 1.0f,
    -1.0f , 0.0f , 1.0f
};
相关问题