你如何防止不断循环#include语句?

pdi*_*ley 1 c++ visual-c++

我有两个类的问题,每个类需要知道其头文件中的另一个.

Structure.h

#ifndef STRUCTURE_H
#define STRUCTURE_H

#include <vector>
#include "Sprite.h"
#include "Bullet.h"

class Structure: public Sprite{
public:
Structure(const BITMAP bm, const Vec2& pos, const Vec2& vel,
              const RECT boundaries, std::vector<std::vector<bool>> holes);
virtual ~Structure() = 0;

void takeDamage(const Bullet* bullet);
protected:
std::vector<std::vector<bool>> mBulletHoles;
};

#endif
Run Code Online (Sandbox Code Playgroud)

Structure.cpp

#include "Structure.h"

Structure::Structure(const BITMAP bm, const Vec2& pos, const Vec2& vel,
                     const RECT boundaries, std::vector<std::vector<bool>> holes)
    :Sprite(bm, pos, vel, boundaries),
     mBulletHoles(holes)
{}

void Structure::takeDamage(const Bullet* bullet){

}

Sprite::~Sprite(){}
Run Code Online (Sandbox Code Playgroud)

Bullet.h

#ifndef BULLET_H
#define BULLET_H

#include "Animate.h"
#include "Structure.h"

class Bullet: public Sprite{
public:
Bullet(const BITMAP bm, const Vec2& pos, const Vec2& vel, const RECT boundaries, 
           bool friendly);
virtual ~Bullet();

int checkCollision(Animate* target);
void checkCollision(Structure* target, float dt);

private:
float mTimeSinceCollision;

bool mFriendly;
bool mActive;

const static float mPenetrationTime;
};

#endif
Run Code Online (Sandbox Code Playgroud)

Bullet.cpp

#include "Bullet.h"

Bullet::Bullet(const BITMAP bm, const Vec2& pos, const Vec2& vel,
               const RECT boundaries, bool friendly)
:Sprite(bm, pos, vel, boundaries),
 mTimeSinceCollision(0.0f),
 mFriendly(friendly),
 mActive(true)
{}

int Bullet::checkCollision(Animate* target){
int returnPoints = 0;
if((target->identifier() == "Player") && !mFriendly){
    if(isTouching(target)){
        target->takeDamage();
        mActive = false;
    }
}else if((target->identifier() == "Alien") && mFriendly){
    if(isTouching(target)){
        returnPoints = target->takeDamage();
        mActive = false;
    }
}
return returnPoints;
}

void Bullet::checkCollision(Structure* target, float dt){
if(isTouching(target)){
    mTimeSinceCollision += dt;
    target->takeDamage(this);
}
if(mTimeSinceCollision >= mPenetrationTime){
    mActive = false;
}
}

Bullet::~Bullet(){}

const float Bullet::mPenetrationTime = .05;
Run Code Online (Sandbox Code Playgroud)

因为两个头文件互相调用,我收到很多错误.我累了更换

#include "Bullet.h"
Run Code Online (Sandbox Code Playgroud)

在Structure.h中

class Bullet;
Run Code Online (Sandbox Code Playgroud)

但后来编译器说我有多个定义的类型.你是如何绕过循环#include语句的?

Rav*_*ine 5

首先,你要避免那些循环包含.然后,如果真的没有出路,你只需在标题中声明所需的类.

例如,在Bullet.h中:

#include "Sprite.h"

class Structure; // Can now use Structure* and Structure&
class Bullet {
...
}
Run Code Online (Sandbox Code Playgroud)

在Bullet.cpp中

#include "Bullet.h"
#include "Structure.h"
Run Code Online (Sandbox Code Playgroud)

在Structure.h中:

#include "Sprite.h"

class Bullet;   // Can now use Bullet* and Bullet&
class Structure {
...
}
Run Code Online (Sandbox Code Playgroud)

在Structure.cpp中

#include "Structure.h"
#include "Bullet.h"
Run Code Online (Sandbox Code Playgroud)

当编译器在Bullet实现中看到未知的"Sprite"对象时,他会知道你指的是一个特定的对象,因为你在头文件中声明了.例如,请参阅C++ FAQ Lite.