我试图在我的类构造函数中初始化指向struct数组的指针,但它根本不起作用...
class Particles {
private:
struct Particle {
double x, y, z, vx, vy, vz;
};
Particle * parts[];
public:
Particles (int count)
{
parts = new Particle [count]; // < here is problem
}
};
Run Code Online (Sandbox Code Playgroud)
[]从声明中删除它们.它应该是
Particle *parts;
Run Code Online (Sandbox Code Playgroud)
使用C++,您可以使用以下优点std::vector:
class Particles {
// ...
std::vector<Particle> parts;
public:
Particles (int count) : parts(count)
{
}
};
Run Code Online (Sandbox Code Playgroud)