我正在尝试为Bullet物理编写一个包装器,以便在我的游戏框架中实现。
我有3个类,mDebugDraw是项目调试图的实现。
mRigidBody是子弹刚体的容器。
世界是子弹世界的容器。
我在visual studio中有大约100个错误,而且几乎所有错误都来自我正在尝试创建mRigidBodies的地方,错误是“无法从初始化列表转换为mRigidBody”,“没有适当的构造函数可用”,如以及像“mRigidBody :: mRigidBody(std :: string)成员函数已经定义或声明的那些奇怪的”,这不是我实现的函数。
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 ==========
在此先感谢您的帮助,我一直在努力解决这一问题。
答案 0 :(得分:1)
World
在声明之前使用。在mRigidBody
的构造函数的定义中,您引用了World::shapeTypes
,但World
仅在文件的后面定义。尝试在World
之前放置mRigidBody
的定义。在定义mRigidBody
之前,您可能需要forward declare World
。
答案 1 :(得分:1)
在mRigidBody类定义之前插入一行:
class World;
class mRigidBody