构造函数无法找到合适的定义,以及 100 个无处不在的其他错误?

Constructor can't find appropriate definition, and 100 other errors out of nowhere?

我正在尝试为 Bullet Physics 编写一个包装器,以便在我的游戏框架中实施。

我有3个类,mDebugDraw是子弹调试绘图的实现

mRigidBody 是子弹刚体材料的容器。

World 是子弹世界内容的容器。

我在 visual studio 中有大约 100 个错误,几乎所有错误都来自我尝试创建 mRigidBodies 的地方,错误是 "cannot convert from initializer list to mRigidBody"、"no appropriate constructor available",因为以及像 "mRigidBody::mRigidBody(std::string) member function is already defined or declared" 这样的奇怪功能,这不是我实现的功能。

world.h 包含这三个 类 的定义:

#pragma once
#include <btBulletDynamicsCommon.h>
#include <BulletCollision\CollisionShapes\btBoxShape.h>
#include <GL/glew.h>
#include "LinearMath\btIDebugDraw.h"
#include <vector>
#include <glm\vec3.hpp>
#include <iostream>
#include <map>

class mDebugDraw : public btIDebugDraw
{
private:
    int debugMode;
public:
    mDebugDraw();
    virtual ~mDebugDraw();

    struct mLine
    {
        glm::vec3 from;
        glm::vec3 to;
        glm::vec3 color;

        mLine(const glm::vec3 ifrom, const glm::vec3 ito, glm::vec3 _color)
        {
            from = ifrom;
            to = ito;
            color = _color;

        }
    };

    std::vector<mLine> lines;

    struct mColor
    {
        glm::vec3 col;

        mColor(const glm::vec3 c)
        {
            col = c;
        }
    };

    std::vector<mColor> colors;

    GLuint vao;
    GLuint vbo[2];

    virtual void drawLine(const btVector3& from, const btVector3& to, const btVector3& color);

    virtual void drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB, btScalar distance, int lifeTime, const btVector3& color);

    virtual void draw3dText(const btVector3& location, const char* textString);

    virtual void setDebugMode(int m_debugMode);

    virtual int getDebugMode() const;

    virtual void drawTriangle(const btVector3 & a, const btVector3 & b, const btVector3 & c, const btVector3 & color, btScalar alpha) {}

    void reportErrorWarning(const char * warningString) { std::cout << "Physics debugger warning: " << warningString << std::endl; }

    std::vector<mLine> & GetLines() { return lines; }

    void draw();

    void clean();
};

class mRigidBody
{
public:
    mRigidBody(std::string name, World::shapeTypes type, glm::vec3 extent, glm::vec3 startposition);
    mRigidBody(std::string name, World::shapeTypes type, glm::vec3 extent, glm::vec3 startPosition, float mass, float friction, float restitution);
    void setPosition(glm::vec3 _position);
    void setMass(float _mass);
    void setRestitution(float _restitution);
    void setFriction(float _friction);
    std::string getName() const { return name; }
    btCollisionShape* getShape() { return mShape; }
    btRigidBody* getBody() { return rigidBody; }
    int getIndex() const { return index; }
private:
    btCollisionShape* mShape;
    btRigidBody* rigidBody;
    std::string name;
    int index;
    //btDefaultMotionState * mMotionState;
};

class World
{

protected:
    World();
public:
    enum shapeTypes
    {
        sphere,
        cube,
        capsule
    };
    static World * gameWorld();
    void init();
    btDiscreteDynamicsWorld* physicsWorld;
    std::vector<btBoxShape> hitboxes;
    mDebugDraw debugDrawer;
    void drawWireframe();
    mRigidBody* getRigidBody(std::string name) const;
    void addRigidBody(mRigidBody* _body, std::string _name);
    std::map<std::string, mRigidBody *> getMap() { return mRigidBodies; }
private:
    static World *mInstance;
    std::map<std::string, mRigidBody *> mRigidBodies;
};

大部分错误来自 world.h 中的这两行,我在其中定义了两个构造函数。

mRigidBody(std::string name, World::shapeTypes type, glm::vec3 extent, glm::vec3 startposition);
mRigidBody(std::string name, World::shapeTypes type, glm::vec3 extent, glm::vec3 startPosition, float mass, float friction, float restitution);

这是完整的错误列表。

1>  main.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  World.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>c:\repo\bustle\src\world.cpp(52): error C2511: 'mRigidBody::mRigidBody(std::string,World::shapeTypes,glm::vec3,glm::vec3)': overloaded member function not found in 'mRigidBody'
1>  c:\repo\bustle\include\world.h(73): note: see declaration of 'mRigidBody'
1>c:\repo\bustle\src\world.cpp(62): error C2597: illegal reference to non-static member 'mRigidBody::mShape'
1>c:\repo\bustle\src\world.cpp(65): warning C4244: 'argument': conversion from 'glm::tvec3<float,0>::length_type' to 'btScalar', possible loss of data
1>c:\repo\bustle\src\world.cpp(65): error C2597: illegal reference to non-static member 'mRigidBody::mShape'
1>c:\repo\bustle\src\world.cpp(68): error C2597: illegal reference to non-static member 'mRigidBody::mShape'
1>c:\repo\bustle\src\world.cpp(74): error C2597: illegal reference to non-static member 'mRigidBody::mShape'
1>c:\repo\bustle\src\world.cpp(74): error C2512: 'btRigidBody': no appropriate default constructor available
1>  c:\repo\bustle\bullet\src\bulletdynamics\vehicle\btwheelinfo.h(17): note: see declaration of 'btRigidBody'
1>c:\repo\bustle\src\world.cpp(76): error C2671: 'mRigidBody::{ctor}': static member functions do not have 'this' pointers
1>c:\repo\bustle\src\world.cpp(79): error C2511: 'mRigidBody::mRigidBody(std::string,World::shapeTypes,glm::vec3,glm::vec3,float,float,float)': overloaded member function not found in 'mRigidBody'
1>  c:\repo\bustle\include\world.h(73): note: see declaration of 'mRigidBody'
1>c:\repo\bustle\src\world.cpp(80): error C2597: illegal reference to non-static member 'mRigidBody::name'
1>c:\repo\bustle\src\world.cpp(91): error C2597: illegal reference to non-static member 'mRigidBody::mShape'
1>c:\repo\bustle\src\world.cpp(94): warning C4244: 'argument': conversion from 'glm::tvec3<float,0>::length_type' to 'btScalar', possible loss of data
1>c:\repo\bustle\src\world.cpp(94): error C2597: illegal reference to non-static member 'mRigidBody::mShape'
1>c:\repo\bustle\src\world.cpp(97): error C2597: illegal reference to non-static member 'mRigidBody::mShape'
1>c:\repo\bustle\src\world.cpp(104): error C2227: left of '->calculateLocalInertia' must point to class/struct/union/generic type
1>c:\repo\bustle\src\world.cpp(106): error C2597: illegal reference to non-static member 'mRigidBody::mShape'
1>c:\repo\bustle\src\world.cpp(107): error C2671: 'mRigidBody::{ctor}': static member functions do not have 'this' pointers
1>c:\repo\bustle\src\world.cpp(107): error C2227: left of '->rigidBody' must point to class/struct/union/generic type
1>c:\repo\bustle\src\world.cpp(108): error C2671: 'mRigidBody::{ctor}': static member functions do not have 'this' pointers
1>c:\repo\bustle\src\world.cpp(108): error C2597: illegal reference to non-static member 'mRigidBody::name'
1>  State_Tutorial.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  State_MainMenu.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  State_Loading.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  State_Gameplay.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  State_EndRound.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>c:\repo\bustle\src\state_endround.cpp(530): warning C4018: '<': signed/unsigned mismatch
1>  State_BulletTest.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>c:\repo\bustle\src\state_bullettest.cpp(18): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'mRigidBody'
1>  c:\repo\bustle\src\state_bullettest.cpp(18): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\repo\bustle\src\state_bullettest.cpp(18): error C2512: 'mRigidBody': no appropriate default constructor available
1>  c:\repo\bustle\include\world.h(73): note: see declaration of 'mRigidBody'
1>c:\repo\bustle\src\state_bullettest.cpp(19): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'mRigidBody'
1>  c:\repo\bustle\src\state_bullettest.cpp(19): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\repo\bustle\src\state_bullettest.cpp(19): error C2512: 'mRigidBody': no appropriate default constructor available
1>  c:\repo\bustle\include\world.h(73): note: see declaration of 'mRigidBody'
1>  Sprite.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>c:\repo\bustle\src\sprite.cpp(167): warning C4018: '>': signed/unsigned mismatch
1>  Player.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  Passenger.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  Kinematic.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  GameObject.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  GameManager.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>c:\repo\bustle\src\gamemanager.cpp(49): warning C4316: 'State_BulletTest': object allocated on the heap may not be aligned 16
1>  DisplayHandler.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  DebugManager.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  CollisionBoxes.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>  Collision.cpp
1>c:\repo\bustle\include\world.h(75): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(75): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2653: 'World': is not a class or namespace name
1>c:\repo\bustle\include\world.h(76): error C2061: syntax error: identifier 'shapeTypes'
1>c:\repo\bustle\include\world.h(76): error C2535: 'mRigidBody::mRigidBody(std::string)': member function already defined or declared
1>  c:\repo\bustle\include\world.h(75): note: see declaration of 'mRigidBody::mRigidBody'
1>c:\repo\bustle\src\collision.cpp(158): warning C4018: '<': signed/unsigned mismatch
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

在此先感谢您的帮助,我已经为此苦苦挣扎了一天。

World 在声明之前使用。在 mRigidBody 的构造函数的定义中,您引用 World::shapeTypesWorld 仅在文件的后面定义。尝试将 World 的定义放在 mRigidBody 之前。在定义 World.

之前,您可能需要 forward declare mRigidBody

在mRigidBody前插入一行class定义:

class World;
class mRigidBody