-1

What is wrong with this code:

Vertex *transformIntoVertex(float *v, int size){
    int i;
    float x_axis = x_0 + (x_Max/size);
    Vertex *axisVertices2[9];

    for(i = 0; i < size; ++i){
        axisVertices2[i] = {{ 0.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }};
    }       
    return axisVertices2;    }

typedef struct
{
    float XYZW[4];
    float RGBA[4];
} Vertex;

I am getting this error:

 error C2059: syntax error : '{'

The error is on this line:

axisVertices2[i] = {{ 0.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }};

Can you suggest me a macro to fill in with my custom values, i must use constants cannot use variables

4

2 回答 2

1

这条线是错误的

axisVertices2[i] = {{ 0.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }};

赋值{}只能在变量的声明阶段完成

于 2013-04-22T16:16:09.283 回答
0

你会想做这样的事情。请注意,我正在使用malloc为数组创建空间。这将允许我们从这个函数返回值。我还使用了一种有效的方法来分配给axisVertices2.

但是,如果您使用它,transformIntoVertex则负责调用free此函数返回的指针的调用者。

Vertex *transformIntoVertex(float *v, int size){
    int i;
    float x_axis = x_0 + (x_Max/size);
    Vertex *axisVertices2 = malloc(sizeof(Vertex) * size);

    for(i = 0; i < size; ++i){
        axisVertices2[i].XYZW[0] = 0.0;
        axisVertices2[i].XYZW[1] = 0.0;
        axisVertices2[i].XYZW[2] = 0.0;
        axisVertices2[i].XYZW[3] = 1.0;

        axisVertices2[i].RGBA[0] = 1.0;
        axisVertices2[i].RGBA[1] = 1.0;
        axisVertices2[i].RGBA[2] = 1.0;
        axisVertices2[i].RGBA[3] = 1.0;
    }

    return axisVertices2;
}

从技术上讲,你也可以做这样的事情,但我怀疑你会看到很多性能提升。我认为第一个版本更明显,至少在这一点上。

Vertex *transformIntoVertex(float *v, int size){
    int i;
    float x_axis = x_0 + (x_Max/size);
    Vertex *axisVertices2 = malloc(sizeof(Vertex) * 9);

    static const Vertex default_value = {{0.0, 0.0, 0.0, 1.0}, {1.0, 1.0, 1.0, 1.0}};

    for(i = 0; i < size; ++i){
        memcpy(&axisVertices2[i], &default_value, sizeof(Vertex));
    }

    return axisVertices2;
}
于 2013-04-22T16:26:17.057 回答