错误2061 - 当我包含标题时,类变为“未定义”

时间:2011-11-04 14:17:52

标签: c++

这很奇怪,我无法理解导致它的原因....所以我有两个类,Player和Enemy以及一个实用程序静态类CollisionMgr。

我在世界级中有一个敌​​人阵列,我想在Player类中有一个指向它的指针用于碰撞检测。但是当我尝试#include玩家中的敌人类时,我在敌人中得到“玩家未定义”错误......我认为它与头部包含有关...

    #pragma once
    #include <string>
    #include <vector>

    #include "mathlib.h"
    #include "md2model.h"
    #include "Object3DS.h"



    const float PLAYER_FORWARD_SPEED = 60.0f;
    const float PLAYER_HEADING_SPEED = 60.0f;

    class Player : public GameObject
    {
    public:

        Player(float r);
        ~Player();

        void loadModel(const char *filename);
        void drawModel();
        void drawBoundingWireframe();
        void passVisibleObjects(vector<Object3DS*> &vo);

    private:
    Entity3D entity;
MD2Model* player;

float modelRot;
float modelPitch;

Vector3 scale;
Vector3 pos;
Vector3 rot;

float forwardSpeed;
float heading;

float radius;


vector<Object3DS*> visibleObjects;

    };

这就是敌人类

    #pragma once
    #include <string>
    #include <vector>

    #include "Player.h"
    #include "mathlib.h"
    #include "md2model.h"


    const float ENEMY_FORWARD_SPEED = 60.0f;
    const float ENEMY_HEADING_SPEED = 60.0f;

    class Enemy : public GameObject
    {
    public:

Enemy(Player* p, float r);
~Enemy();

void loadModel(const char *filename);
void drawModel();
void drawBoundingWireframe();


bool checkCollision();

void setPosition(Vector3 pos);
void setRotation(Vector3 r);
void setScale(Vector3 r);


void setSpeed(float s);
void setHeading(float h);
float getForwardSpeed();
float getHeading();
void rotateModel(float dx, float dy);
void resetRotation();

void setAnimation(char *filename);

Vector3 getPosition();
void moveEnemy(float elapsedTimeSec);

Entity3D entity;

void passVisibleObjects(vector<Object3DS*> &vo);



    private:
MD2Model* enemy;

Player* player;

float modelRot;
float modelPitch;

Vector3 scale;
Vector3 pos;
Vector3 rot;

float forwardSpeed;
float heading;

float radius;

vector<Object3DS*> visibleObjects;

    };

在源文件中,我包含了一些带有一些碰撞函数的静态类:

    #pragma once


    #include <vector>
    #include "mathlib.h"
    #include "Object3DS.h"

    #include "Enemy.h"

    class CollisionMgr{


    public:

static vector<Object3DS*> findVisibleObjects(BoundingBox &viewingVolume,                 vector<Object3DS> &gameObjects, vector<Object3DS*> &visibleObjects);
static vector<Enemy*> findVisibleEnemies(BoundingBox &viewingVolume, vector<Enemy> &enemies, vector<Enemy*> &visibleEnemies);
static bool checkCollision(const Vector3 pos, const Vector3 forward, float radius, const vector<Object3DS*> &vo);
static double intersectRaySphere(Vector3 rO, Vector3 rV, Vector3 sO, double sR);


    };

所有这些编译都是这样的,但是当我尝试在播放器中添加#include“Enemy”的那一刻我得到错误C2061:语法错误:Enemy类中的标识符'Player'....或者如果我成功包含敌人,然后尝试包括CollisionMgr ....他们之间有两种冲突和Player的定义,但我不明白如何解决它....

使用VS2010作为我的编译器..

任何帮助将不胜感激!

我尝试重新安排include语句,但我真的不明白是什么导致了这个?

1 个答案:

答案 0 :(得分:4)

这是循环依赖的情况。您需要正确转发声明PlayerEnemy,否则您将无法在相反的头文件中使用它们的定义。您可以在实现文件中使用它们。这是一个例子:

Player.h

class Player
{
   // needs to know about Enemy
   void f(const Enemy& b);
};

Enemy.h

class Enemy
{
   // needs to know about Player 
   void f(const Player& a);
};

在两个位置包含头文件都会导致错误(就像您所见):

Player.h

// This isn't going to work...
#include "Enemy.h"
class Player
{
   // needs to know about Enemy
   void f(const Enemy& b);
};

Enemy.h

// This isn't going to work...
#include "Player.h"

class Enemy
{
   // needs to know about Player 
   void f(const Player& a);
};

这里需要PlayerEnemy的“前瞻性声明”,如下所示:

Player.h

// forward declaration:
class Enemy;

class Player
{
   // needs to know about Enemy
   void f(const Enemy& b);
};

Enemy.h

// forward declaration
class Player;

class Enemy
{
   // needs to know about Player 
   void f(const Player& a);
};

然后在实现文件中,您可以#include相应的标题来实际获取编译单元中对象的定义。