Public Group

# is there a less stupid way to initialize a vector?

This topic is 3069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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());}

##### Share on other sites
Quote:
 Original post by draconarstd::vector particles(1000);but it won't compile.

Then Particle does not have a visible default constructor or is an incomplete type at this point.

##### Share on other sites
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..

##### Share on other sites
Post a complete, but minimal, code sample that demonstrates your problem.

##### Share on other sites
Quote:
 Original post by SiCranePost 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)    };

##### Share on other sites
Seems like you're initializing something in a declaration.

##### Share on other sites
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) {    // ...}

##### Share on other sites
Quote:
 Original post by mattdThat 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.Changestd::vector particles(1000);to just:std::vector particles;Have this as the class's constructor:Square::Square() : particles(1000) { // ...}

Worked like a charm. Thank you very much you all, guys!

##### Share on other sites
I think you may have gotten confused with C#, which does allow the kind of initialisation in the manner you were doing, IIRC.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001962
×