我正在尝试做一些非常简单的事情,但我做错了什么。
头文件:
class Example
{
public:
typedef struct
{
float Position[3];
float Color[4];
float TexCoord[2];
} IndicatorVertex;
void doSomething();
};
.cpp文件:
void Example::doSomething()
{
IndicatorVertex *vertices;
vertices = IndicatorVertex[] {
{{-1.0, 1.0, 1.0}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
{{1.0, 1.0, 1.0}, {1.0f, 1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
};
}
编译后,我得到
Error:(12, 13) unexpected type name 'IndicatorVertex': expected expression
。(我故意不使用
std::vector
等;我故意在c++ 11设置中使用C功能。) 最佳答案
您无法像创建动态数组一样,需要定义一个实际的数组,例如
IndicatorVertex vertices[] = { ... };
如果以后需要指针,请记住,数组自然会衰减到指向其第一个元素的指针。因此,例如,如果您想调用一个需要
IndicatorVertex*
参数的函数,只需传入vertices
,它仍将按预期运行。如果要具有不同的数组并使
vertices
指向其中之一,则必须定义如上所示的数组,并使vertices
指向其中之一。喜欢IndicatorVertex vertices1[] = { ... };
IndicatorVertex vertices2[] = { ... };
// ...
IndicatorVertex* vertices = vertices1;
// ...
vertices = vertices2;
关于c++ - 用于结构的动态聚合初始化的正确形式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41719193/