C++ 类不生成匹配函数和已知转换错误

标签 c++ compiler-errors box2d

我一直在学习 Box2D 和 C++,并设法在测试台中创建了一个简单的模拟,现在我正尝试将模拟从测试台中取出并将其与 SDL shell 集成。

但是,当我尝试实例化它时,以前在测试平台上工作的类现在会产生错误,我有点困惑为什么它在测试平台上工作得很好,但现在却抛出了变量转换错误。

这是我的课:

    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) ===|

最佳答案

行内

Ball* ball = new Ball(world, 1);

您使用了一个不存在的 Ball 构造函数,因为唯一可用的是 Ball(b2World* world, float radius) 和复制构造函数。如果你想使用你声明的构造函数,你需要传递指向世界的指针:

Ball* ball = new Ball(&world, 1);

关于C++ 类不生成匹配函数和已知转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22221265/

相关文章:

c++ - Visual Studio 调试 - 局部变量的顺序?

未找到 Java 包 : Compiling with CPlex from command line

android - Box2d libgdx 在设备上非常慢

actionscript-3 - AS3 > 鼠标事件没有发生?

c++ - Box2D b2World 类

android - Clang 和 Visual C++ 结构对齐兼容性问题

c++ - 清除 __m128i 的高位字节

C++ 映射插入不良行为

compiler-errors - 如何在Travis CI容器中安装具有webp支持的较新imagemagick?

java - Eclipse -- SharedClasses 存在构建路径错误