如何更正类型对象错误

时间:2013-10-31 22:06:21

标签: c++

这是我的其中一个类的代码,不幸的是我不断收到错误消息,说“武器不是一个类型'我不知道这会是什么类型的。

knight.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 }

2 个答案:

答案 0 :(得分:3)

这里的错误是你没有宣布武器是什么。

你有一个忘了包含的头文件吗?

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

答案 1 :(得分:3)

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