【发布时间】:2016-03-08 09:39:18
【问题描述】:
我正在尝试通过多个头文件和 cpp 文件为我正在编写的文本游戏使用继承。
我有自己的基类武器。在文件 Weapon.h 中
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
然后我尝试从基础 Weapon.h 类继承到 Bow and Sword 类。我确定我正确地包含了该文件,但是当我尝试编译时,我得到了错误 "error: expected class name class Blade : public Weapon" Bow 类的相同错误。
#include "Weapon.h"
#include "Crossbow.h"
using namespace std;
class Bow : public Weapon
{
public:
string Type = "Ranged";
bool loaded;
protected:
Bow();
};
#include "Weapon.h"
class Blade : public Weapon
{
private:
string Type = "Melee";
protected:
void Draw();
};
有人知道为什么会这样吗?谷歌也没有提供任何有用的东西。谢谢
MCVE(我认为)
//In Base.h
class Base
{
public:
int function();
private:
};
//In Base.cpp
int Base::function()
{
randomshit
return 0;
}
//In Inherit.h
#include "Base.h"
class Inherit : public Base
{
public:
int function():
private:
};
Getting error: "expected class name class Bow : public Weapon"
编辑:原来我需要包含“#pragma once”,这几乎解决了所有问题。谢谢大家的帮助。
【问题讨论】:
-
您在哪一行得到错误?
crossbow.h中有什么内容? -
可能是循环包含依赖项。但是你为什么
#include "CrossBow.h"? -
@MichaelWalz 我在“class Bow”行中收到错误,与 Blade 类相同。对于弩,我继承弓类。我只是在其中包含了#include,因为我想不出任何其他可以解决我的问题的方法。我在 Crossbow.h 文件中也遇到了问题。出于某种原因,当我在同一个文件中继承时,我没有遇到问题。
-
@juanchopanza 我想不出还有什么可做的,所以我开始随机包含其他文件,希望它能修复它,但我还没有删除它们。同样由于某种原因,当我从同一个文件中继承时,我没有遇到问题。
-
你可以添加错误吗?
标签: c++ class inheritance