Create B2World inside class definition

I can't figure out why I am getting errors when I attempt to create a b2World in a header file. Apparantly, you can't create a b2World without setting the Gravity. This is essentially what I have :

Code:
 b2Vec2 gravity(0.0f, 10.0f);

class Box2D_Component
{
public:
    Box2D_Component

private:
    b2World world(gravity);
    float timestep;
    int32 velocityIterations;
    int32 positionIterations;
    uint32 flags;
}



I get this error :
Quote:
error: 'gravity' does not name a type


My reasoning for doing this is so that any part of this class can access my world object. Does anyone know a way to do this?
This is a generic C++ question really...
When you give a parameter list like that you are actually declaring a function, and the compiler expects to know the type for each function parameter. So it wants to know what type 'gravity' is. If this was supposed to be a function you would need to make it:

b2World world( b2Vec2 gravity );

... but since it sounds like you want it to be a member variable then it would be:

b2World world;

I say 'would be' because you have probably tried that and found that the class could not be instantiated like this because it has no default constructor. To be honest what you really need to do is more study on the basics of classes and how they get constructed/destructed. That's the "teach a man to fish" answer 🙂

The "give a man a fish" answer is that you can make the class member variable a pointer, so that it does not get instantiated until later:
Code:
private:
    b2World* world;

public:
    Box2D_Component();

Then in the .cpp file you would typically instantiate the 'world' variable in the class constructor:
Code:
Box2D_Component() {
    world = new b2World( gravity );
}
Thanks for the Answer. I tried this before, and got an error. That is why I turned back to finding a different way. I did exactly what you said above, and I get this :

Code:
undefined reference to 'b2World::b2World'(b2Vec2 const&)'
Wow, nevermind. I forgot that Box2D isn't a header-only library and thus didn't included the library, doh. Thanks again!

Original thread: https://box2d.org/forum/viewtopic.php?t=8826