is there a less stupid way to initialize a vector?

Started by
10 comments, last by SiCrane 14 years, 1 month ago
Here, I know that for a vector of a "primitive" type, we can initialize it using:

vector<int> myInts(100);
the vector will be initialized with a 100 spaces filled with zeros. Now, I'm playing with a particle system using OpenGL and I want to do something like this:


std::vector<Particle> particles(1000);
but it won't compile. the type Particle is a class using the default constructors and destructors, and filled with hand-made, declared "default" variables. This class could be a struct by any means... As it won't compile, I'm initializing the vector using


    for(int i = 0; i < MAX_PARTICLES; i++){
        Particle *particle = new Particle();
        particles.push_back(*particle);
    }

any help is welcomed! cheers, Fabio
Advertisement
Compilers generally give error messages when they refuse to compile code. What error message are you getting?


The following code leaks memory like a sieve... full of memory.
for(int i = 0; i < MAX_PARTICLES; i++){    Particle *particle = new Particle();    particles.push_back(*particle);}

You know you can do this:
for(int i = 0; i < MAX_PARTICLES; i++){    // Pass a temporary particle to push_back()    particles.push_back(Particle());}
Quote:Original post by draconar
std::vector<Particle> particles(1000);

but it won't compile.

Then Particle does not have a visible default constructor or is an incomplete type at this point.
here is the error message when I use
std::vector<Particle> particles(1000);

main.cpp:130: error: expected identifier before numeric constant
main.cpp:130: error: expected ‘,’ or ‘...’ before numeric constant

DevFred, the class Particle hasn't a default constructor. Now I have provided one and the error continues..

Post a complete, but minimal, code sample that demonstrates your problem.
Quote:Original post by SiCrane
Post a complete, but minimal, code sample that demonstrates your problem.

here it goes:

//The squareclass Square{    private:    //The offsets    int x, y;    //The velocity of the square    int xVel, yVel;    public:    //Initializes    Square();    //Handles key presses    void handle_input();    //Moves the square    void move();    //Shows the square on the screen    void show();    std::vector<Particle> particles(1000);    std::vector<Particle>::iterator particle_iter;    };

I want the vector Particle to be a member of Square (as in composition).

The Particle class (defined above Square)
class Particle{public:        Particle(){    active = true; /* Active (Yes/No) */    life = 1.0;   /* Particle Life   */    fade = ( float )( rand( ) %100 ) / 1000.0f + 0.003f;   /* Fade Speed      */    r = colors[0][0];      /* Red Value       */    g = colors[11][1];      /* Green Value     */    b = colors[5][0];      /* Blue Value      */    x = 0.0;      /* X Position      */    y = 0.0;      /* Y Position      */    z = 0.0;      /* Z Position      */    xi = 0.0;     /* X Direction     */    yi = 0.0;     /* Y Direction     */    zi = 0.0;     /* Z Direction     */    xg = 0.0;     /* X Gravity       */    yg = -.8;     /* Y Gravity       */    zg = 0.0;     /* Z Gravity       */    }   PARTICLE MEMBERS (vars) ARE HERE (NOT SHOWN)    };

Seems like you're initializing something in a declaration.
That isn't how you use a non-default constructor to construct a member of a class.

You do it in the class's initializer list.

Change
std::vector<Particle> particles(1000);
to just:
std::vector<Particle> particles;

Have this as the class's constructor:
Square::Square() : particles(1000) {    // ...}
Quote:Original post by mattd
That isn't how you use a non-default constructor to construct a member of a class.

You do it in the class's initializer list.

Change
std::vector<Particle> particles(1000);
to just:
std::vector<Particle> particles;

Have this as the class's constructor:
Square::Square() : particles(1000) {    // ...}


OUCH. that hurts. have completely forgotten about this.

Worked like a charm. Thank you very much you all, guys!
I think you may have gotten confused with C#, which does allow the kind of initialisation in the manner you were doing, IIRC.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement