0

这是我的其中一个类的代码,不幸的是,我一直收到一个错误,说“武器不是一种类型”,我知道这只是不确定这将是什么类型。

骑士.h

  1 #ifndef KNIGHT_H
  2 #define KNIGHT_H
  3 
  4 using namespace std;
  5 
  6 class knight
  7 {
  8   private:
  9     string name;
 10     int stamina;
 12     weapon weapon_in_hand(string weapon_type, int sr, int hc);*The problem is here*
 13 
 14   public:
 15     void on_horse();
 16     knight(string name, int stamina, string weapon_type, int sr,int hc);
 17     bool hit();
 18     void unhorse_yourself();
 19     bool are_you_exhausted();
 20     void display();
 21 };
 22 #endif
~                                                                               
"knight.h" 22L, 418C                                          1,1           All

这就是它所连接的

 25 bool hit()
 26 {
 27   stamina=stamina-weapon_in_hand.stamina_required();
 28   if(weapon_in_hand.did_you_hit()==true)
 29     return true;
 30   else
 31     return false;
 32 knight::knight(string n, int st, string weapon_type, int sr,int hc)
 33   :name(n), stamina(st), weapon_in_hand(weapon_type, sr, hc)
 34 {
 35 }
4

2 回答 2

3

这里的错误是你没有声明什么是武器。

您是否有忘记包含的头文件?

编译器会为您编写的每个 .c/.cpp 文件重新开始,因此请确保您 #include 标头以获取您正在寻找的类型定义。

于 2013-10-31T22:09:40.430 回答
3

类型weapon未定义。您必须在 knight.h 的顶部包含武器.h(就在使用之前)。如果这不存在,则必须创建此类。

于 2013-10-31T22:11:15.840 回答