我一直在学习Box2D& C ++并设法在测试平台中创建一个简单的模拟,现在我正在尝试将模拟从测试平台中取出并将其与SDL shell集成。
然而,以前在testbed中工作的类现在在我尝试实例化时会产生错误,而且我有点困惑为什么它在测试平台中完美运行但现在抛出了可变转换错误。
这是我的班级:
class Ball {
public:
bool m_contacting;
b2Body* m_body;
float m_radius;
public:
// Ball class constructor
Ball(b2World* world, float radius) {
m_contacting = false;
m_body = NULL;
m_radius = radius;
//set up dynamic body, store in class variable
b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody;
myBodyDef.position.Set(0, 20);
m_body = world->CreateBody(&myBodyDef);
//add circle fixture
b2CircleShape circleShape;
circleShape.m_p.Set(0, 0);
circleShape.m_radius = m_radius; //use class variable
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &circleShape;
myFixtureDef.density = 1;
myFixtureDef.restitution = 0.83f;
m_body->CreateFixture(&myFixtureDef);
m_body->SetUserData( this );
m_body->SetGravityScale(5);//cancel gravity (use -1 to reverse gravity, etc)
}
~Ball(){}
};
这是我的计划:
//FooTest class member variable
std::vector<Ball*> balls;
b2Body* body;
int main (int argc, char* argv[]) {
// Define the gravity vector.
b2Vec2 gravity(0.0f, -10.0f);
// Construct a world object, which will hold and simulate the rigid bodies.
b2World world(gravity);
//add ball entity to scene in constructor
Ball* ball = new Ball(world, 1); // Fails here
balls.push_back(ball);
// Prepare for simulation. Typically we use a time step of 1/60 of a
// second (60Hz) and 10 iterations. This provides a high quality simulation
// in most game scenarios.
float32 timeStep = 1.0f / 60.0f;
int32 velocityIterations = 6;
int32 positionIterations = 2;
// This is our little game loop.
for (int32 i = 0; i < 60; ++i)
{
// Instruct the world to perform a single step of simulation.
// It is generally best to keep the time step and iterations fixed.
world.Step(timeStep, velocityIterations, positionIterations);
// Now print the position and angle of the body.
b2Vec2 position = body->GetPosition();
float32 angle = body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
// When the world destructor is called, all bodies and joints are freed. This can
// create orphaned pointers, so be careful about your world management.
return 0;
}
这是生成的错误:
C:\Users\Chris\My Programs\_C++\Keepie Uppie\main.cpp||In function 'int main(int, char**)':|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\main.cpp|20|error: no matching function for call to 'Ball::Ball(b2World&, int)'|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\main.cpp|20|note: candidates are:|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\objects.h|15|note: Ball::Ball(b2World*, float)|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\objects.h|15|note: no known conversion for argument 1 from 'b2World' to 'b2World*'|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\objects.h|7|note: Ball::Ball(const Ball&)|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\objects.h|7|note: candidate expects 1 argument, 2 provided|
如果我像这样调用构造函数
Ball* ball = new Ball(&world, 1);
我收到以下错误
obj\Debug\main.o||In function `main':|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\main.cpp|17|undefined reference to `b2World::b2World(b2Vec2 const&)'|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\main.cpp|35|undefined reference to `b2World::Step(float, int, int)'|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\main.cpp|47|undefined reference to `b2World::~b2World()'|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\main.cpp|47|undefined reference to `b2World::~b2World()'|
obj\Debug\main.o||In function `ZN13b2CircleShapeC1Ev':|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\Box2D\Collision\Shapes\b2CircleShape.h|65|undefined reference to `vtable for b2CircleShape'|
obj\Debug\main.o||In function `ZN4BallC1EP7b2Worldf':|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\objects.h|24|undefined reference to `b2World::CreateBody(b2BodyDef const*)'|
C:\Users\Chris\My Programs\_C++\Keepie Uppie\objects.h|34|undefined reference to `b2Body::CreateFixture(b2FixtureDef const*)'|
obj\Debug\main.o||In function `ZN13b2CircleShapeD1Ev':|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\Box2D\Collision\Shapes\b2CircleShape.h|25|undefined reference to `vtable for b2CircleShape'|
||=== Build finished: 8 errors, 0 warnings (0 minutes, 2 seconds) ===|
答案 0 :(得分:2)
在第
行Ball* ball = new Ball(world, 1);
您使用的Ball
构造函数不存在,因为唯一可用的构造函数是Ball(b2World* world, float radius)
和复制构造函数。如果要使用您声明的构造函数,则需要将指针传递给world:
Ball* ball = new Ball(&world, 1);
答案 1 :(得分:1)
您将构造函数声明为具有以下参数
Ball(b2World* world, float radius);
这是第一个参数pointer to 2World
。但是你使用2World而不是指针
b2World world(gravity);
//add ball entity to scene in constructor
Ball* ball = new Ball(world, 1);
世界不是指向b2World的指针。所以这句话
Ball* ball = new Ball(world, 1);
无效。
也许你的意思
Ball* ball = new Ball( &world, 1 );