我想做vertices
一个数组Vector
。但我不知道如何声明和初始化它。我试过这个,但它抱怨:
class Mesh {
public:
Vector vertices[];
int verticesCount;
Mesh();
virtual ~Mesh();
};
Mesh::Mesh() {
verticesCount = 4;
vertices = new Vector[verticesCount]; // error: expected primary-expression before ']' token
vertices[0] = new Vector(0, 0, 0);
vertices[1] = new Vector(1, 0, 0);
vertices[2] = new Vector(1, 1, 0);
vertices[3] = new Vector(0, 1, 0);
}
Mesh::~Mesh() {
delete vertices;
}
编辑
尝试纠正,应用您的提示,我达到了这一点:
Vector* vertices;
//...
vertices = new Vector[verticesCount];
vertices[0] = Vector(0, 0, 0);
vertices[1] = Vector(1, 0, 0);
vertices[2] = Vector(1, 1, 0);
vertices[3] = Vector(0, 1, 0);
//...
delete[] vertices;
它奏效了。但是可以吗?