Box2D Error

Started by
5 comments, last by LeftyGuitar 10 years, 4 months ago

Hello,

I am having trouble trying to set up my physics class in C++ using Box2D. Every time I try to set the World variable to the Gravity variable, I keep getting this error, 1 IntelliSense: no instance of constructor "b2World::b2World" matches the argument list
argument types are: (b2Vec2 *, bool)

I assume, there is something wrong with converting the variables, but I can't exactly pinpoint it. I'll post my code.


//Physics.CPP
#include "main.h"
#include "physics.h"

Physics::Physics()
{
	X_Grav = 0.0f;
	Y_Grav -= 10.0f;
	Sleep = true;

	Gravity = new b2Vec2(X_Grav,Y_Grav);
	World = new b2World(Gravity,Sleep); //error comes from this
}

//Physics.h
class Physics
{
public:
	Physics();
	~Physics();

	b2Vec2* Gravity;
	b2World* World;

	bool Sleep;

This is only some of the code, I think it is enough to help figure out what the problem is. I can post more code if it is needed. Thanks.

Advertisement
Does it compile? Yes? Then you have nothing to worry about. C++ is complicated to parse, it's a miracle IntelliSense even works at all.

TL;DR: Don't be too trusting of IntelliSense for C++ code.

EDIT:
Misread error messages

No, it does not compile, here is the error from trying to compile Error 1 error C2664: 'b2World::b2World(const b2Vec2 &)' : cannot convert parameter 1 from 'b2Vec2 *' to 'const b2Vec2 &'

It sounds like the constructor for b2World is expecting an actual value (a constant reference), and not a pointer, which is what you are passing in.

EDIT: To test, just stick a * in front of the variable:

World = new b2World(*Gravity,Sleep);

C2664: function, cannot convert parameter number from type1 to type2. In your case, function is the b2World constructor, numer is referring to the first parameter of the constructor, type1 is a pointer to b2Vec, and type2 is a const reference to b2Vec. To fix, you can either simply dereference Gravity or change Gravity to be a b2Vec2 and not a b2Vec2*. For more on the subject, consult your C++ reference on the subject of pointers and references.

If you're coming from a C# or Java background, objects need not be allocated with new:

class Foobar {
public:
    Foobar(int id) {
        a = id;
        ptr = new char[15];
    }

    Foobar( const Foobar& other ) { // copy constructor
        this->a = other.a;
        this->ptr = new char[15];
    }

    Foobar& operator= ( const Foobar& other ) { // assignment (copy)
        this->a = other.a;
        delete[] this->ptr;
        this->ptr = new char[15];

        return *this;
    }

    ~Foobar() {
        delete[] ptr;
    }

private:
    int a;
    char* ptr;
};

int main() {
    Foobar A(1);        // A is a valid Foobar (resides on the stack)
    Foobar& B = A;      // B *is* A
    Foobar* C = &A;     // C is a pointer to A
    Foobar D(A);        // D is a copy of A (uses copy constructor)

    C = &D;             // C now points to D
    B = D;              // A is now a copy of D (uses assignment operator, cannot reassign references)

    Foobar* E = new Foobar(3); // create a new Foobar (residing on the freestore), assign it to E
    C = E;              // C and E refer to the same Foobar
                            
    delete C;           // destroy Foobar #3
    E = 0;              // E still refers to deleted Foobar #3, set to nullptr/NULL/0 to prevent accidental access
}
You probably shouldn't make the Gravity vector in the Physics class a pointer, just use it by value instead.


#include "main.h"
#include "physics.h"

Physics::Physics()
{
	X_Grav = 0.0f;
	Y_Grav -= 10.0f;
	Sleep = true;
        
        // took the 'new' keyword off
	Gravity = b2Vec2(X_Grav,Y_Grav);
	World = new b2World(Gravity,Sleep); //error comes from this
}

//Physics.h
class Physics
{
public:
	Physics();
	~Physics();

        // Don't make the vector a pointer
	b2Vec2 Gravity;
	b2World* World;

	bool Sleep;
There really isn't a good reason to have a small structure like a vector be a pointer. It is much better to pass it around by value, or by reference.

Also, you shouldn't need an X_Grav and Y_Grav since you already have the Gravity vector. Having both is redundant and makes it harder to manage the Physics class because you would have to always try to keep the two values in sync, just use Gravity.x instead of X_Grav.


Physics::Physics()
{
	Sleep = true;
	Gravity = b2Vec2(0.0f,-10.0f);
	World = new b2World(Gravity,Sleep);
}

//Physics.h
class Physics
{
public:
	Physics();
	~Physics();

	b2Vec2 Gravity;
	b2World* World;

	bool Sleep;
My current game project Platform RPG

Ah, I see. That makes sense. Thank you.

This topic is closed to new replies.

Advertisement