只有一个值被添加到向量中

only one value being added into the vectors

本文关键字:向量 添加 有一个      更新时间:2023-10-16

试图在opengl中绘制顶点,但我只能从顶点分配一个元素到TaskTwoVerticesN,请帮助…!我做错了什么?如果您需要所有代码,请告诉我,我可以通过电子邮件发送或张贴在这里

typedef struct Vertex {
    float XYZW[4];
    float RGBA[4];
    void SetCoords(float *coords) {
        XYZW[0] = coords[0];
        XYZW[1] = coords[1];
        XYZW[2] = coords[2];
        XYZW[3] = coords[3];
    }
    void SetColor(float *color) {
        RGBA[0] = color[0];
        RGBA[1] = color[1];
        RGBA[2] = color[2];
        RGBA[3] = color[3];
    }
};
Vertex Vertices[] =
{
    { { 1.0f, 1.0f, 0.0f, 1.0f },{ 0.0f, 0.0f, 0.0f, 1.0f } }, // 0
    { { 0.0f, 1.4f, 0.0f, 1.0f },{ 0.0f, 0.0f, 1.0f, 1.0f } }, // 
};
std::vector<Vertex>TaskTwoVerticesN;
void createObjects(void)
{
    TaskTwoVerticesN.clear();
    // and this, running them one by one in a for loop does not work either
    TaskTwoVerticesN.insert(TaskTwoVerticesN.begin(), Vertices, Vertices + (sizeof(Vertices)/sizeof(Vertices[0])));
}
main(){
    createObjects();
}

实际上,修改你的代码足以运行

int main(){
     createObjects();
     std::cout << ((sizeof(Vertices)/sizeof(Vertices[0]))) << std::endl;
     std::cout << TaskTwoVerticesN[0].XYZW[1] << std::endl;
     std::cout << TaskTwoVerticesN[1].XYZW[1] << std::endl;
     return 0;
}

导致以下输出

2
1
1.4

因此,看起来两个元素都按预期插入了。我真正改变的唯一东西是main(),一些std::cout和删除struct上的类型定义。