【问题标题】:only one value being added into the vectors只有一个值被添加到向量中
【发布时间】:2016-09-24 00:46:35
【问题描述】:

试图在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();
}

【问题讨论】:

    标签: c++ vector struct


    【解决方案1】:

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

    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 和删除结构上的 typedef。

    总之,你的插入很好。

    【讨论】:

    • 我的代码还没有运行...!用 类型的向量进行了测试,它工作正常......虽然不适用于这种类型......也尝试使用 push_back() 并且它也失败了
    • 抱歉耽搁了。发布或发送给我,我很乐意查看。
    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 2014-03-31
    相关资源
    最近更新 更多