Sign in to follow this  
draconar

is there a less stupid way to initialize a vector?

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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Post a complete, but minimal, code sample that demonstrates your problem.

here it goes:


//The square
class 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 this post


Link to post
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 this post


Link to post
Share on other sites
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!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
It's generally less efficient to default construct an object and then change it than to construct it how you want it in the first place.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this