gpt4 book ai didi

c++ - 错误 2061 - 当我包含标题时,类变为 "Undefined"

转载 作者:行者123 更新时间:2023-11-27 23:27:55 25 4
gpt4 key购买 nike

这很奇怪,我无法理解是什么原因造成的......所以我有两个类,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;

};

这是 Enemy 类

    #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);


};

所有这些都可以像这样编译,但是当我尝试在 Player 中添加 #include "Enemy"时,我收到错误 C2061: syntax error : identifier 'Player' inside the Enemy class....或者如果我成功包含Enemy,然后尝试包含 CollisionMgr....他们两个和 Player 的定义之间存在某种冲突,但我不明白如何解决它....

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

如有任何帮助,我们将不胜感激!

我尝试重新安排 include 语句,但我真的不明白是什么原因造成的?

最佳答案

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

播放器.h

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

敌人.h

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

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

播放器.h

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

敌人.h

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

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

这里需要的是适当的PlayerEnemy 的“前向声明”,如下所示:

播放器.h

// forward declaration:
class Enemy;

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

敌人.h

// forward declaration
class Player;

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

然后在实现文件中,您可以#include 适当的 header 以实际获取编译单元中对象的定义。

关于c++ - 错误 2061 - 当我包含标题时,类变为 "Undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8010601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com