【发布时间】:2014-04-10 09:41:45
【问题描述】:
我的游戏有一个 Player 类,它是一个玩家,由用户控制,它会使用武器。武器类在不同的头文件中,但包含对播放器的引用,因此它可以告诉播放器类它必须使用什么动画。
现在我的问题是: 似乎我在引用彼此时做错了什么。我在两个头文件中都创建了前向 decleration,但出现错误:“不允许不完整类型”
我也得到了"cannot convert from Player* const to Weapon",因为我使用这个:m_weapon(Weapon(this)),这是解决问题的尝试。
我的文件:
播放器.h
#ifndef PLAYER_H
#define PLAYER_H
#include "DrawableObject.h"
#include "PlayerState.h"
class Weapon;
class Player : public AnimatableObject
{
private:
std::string m_name; //name of the player
States::PlayerState m_state; //state of the player
Weapon &m_weapon;
public:
Player(std::string name):
AnimatableObject("../../Images/PlayerSheet.png",
sf::Vector2i(64,64),sf::Vector2i(8,8)),
m_name(name),
m_weapon(Weapon(this))
{
m_updateTime = 100;
}
//Update the Player
virtual void Update() override;
};
#endif
武器.h
#ifndef WEAPON
#define WEAPON
#include "DrawableObject.h"
class Player;
class Weapon : AnimatableObject
{
protected:
float m_cooldown;
Player *m_user;
public:
Weapon(Player *user):m_cooldown(0.0f),
m_user(user),
AnimatableObject("",sf::Vector2i(0,0),sf::Vector2i(0,0))
{}
Weapon(Player *user, float f):
m_cooldown(f),
AnimatableObject("",sf::Vector2i(0,0),sf::Vector2i(0,0)),
m_user(user)
{}
virtual void Use(){} //Left Click/Trigger
virtual void AltUse(){} //Right Click/Trigger
};
#endif WEAPON
那么我该如何互相引用和处理头文件呢?
PS。如果有帮助,我会使用 Visual Studio 2012
【问题讨论】:
标签: c++ class header-files