Many thanks. Now i try the std vector.
class droplet
{
public:
int trail[16];
int speed;
int X, Y;
droplet(int x, int y, int spd);
~droplet();
...
}
If droplet::~droplet doesn't do anything you *could* just remove your
custom destructor. Just saying.
vector<droplet> drops[200];
This defines 'drops' to be an array of vectors of droplets. Is this
really what you want?
-------in main()
for(int dDrops=0;dDrops<num;dDrops++)
{
drops[dDrops].push_back(
droplet(rand()%PanelLeftrain + 20,
rand()%(PanelToprain - 30),
rand()%2+1) );
// all ok so far
for(int a=0;a<drops[dDrops]->speed*5+2;a++) //ERROR
if(drops[dDrops]->Y-dDrops>0
|| drops[dDrops]->Y-dDrops<PanelTop-2) //ERROR
{
How should I access speed and X/Y as declared in droplet class?
If 'drops' is of type vector<droplet>[200] (array of vectors)
then 'drops[dDrops]' is of type vector<droplet> (vector). But
std::vector doesn't have an overloaded -> operator which is why
'drops[dDrops]->Y' is ill-formed.
You can access the first element in a vector using front like this:
drops[dDrops].front().Y
But I have the feeling that you don't actually want an array of 200
vectors that store only one droplet each.
vector<droplet> drops[200]; // array of 200 vectors of droplets
vector<droplet> drops(200); // vector of 200 droplets
In the first line [200] is part of the type of drops. In the second
line (200) is the syntax for "calling" the vector's constructor and
passing an int to it. However, unless your droplet class also declares
a default constructor the 2nd line won't work because the vector tries
to create 200 droplets via the default constructor. Since you provide
at least one constructor, the compiler won't generate a default
constructor for you. But you can initialize the vector like this:
vector<droplet> drops(200,droplet(1,2,3));
Now, the vector contains 200 droplet objects which are copies of the
temporary droplet object that is passed as 2nd argument.
Alternativly, you can write
vector<droplet> drops;
for (int i=0; i<10; ++i)
drops.push_back(droplet(1,2,3));
Get a decent C++ book and read it, seriously. Search the web for C++
book reviews and pick one that suits you best. This way you can save a
lot of time in the long run. Or, you could just continue this trial
and error. But you're likely to shoot yourself in the foot if you do.
Your call...
Cheers,
SG