Error in code, need help fixing

Doing a project but can not get it to compile..
Can't figure out what my errors are...
here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<iostream>
  2 #include<string>
  3 #include"Random.h"
  4 using namespace std;
  5 
  6 class Weapon{
  7   public:
  8     int hit_chance;
  9     int stamina_required;
 10     string weapon_type;
 11     void display (void);
 12     Weapon(string type, int sr, int hc);
 13     int get_stamina_required(void);
 14     bool did_you_hit(void);
 15 };
 16 
 17 void Weapon::display(void)
 18 {
 19   cout<< "hit chance is" << hit_chance << endl;
 20   cout<< "stamina required is" << stamina_required << endl;
 21   cout<< "weapon type is" << weapon_type<< endl;
 22 
 23 }
 24 
 25 int Weapon::get_stamina_required(void)
 26 {
 27    return stamina_required;
 28 }
 29 
 30 bool did_you_hit(void)
 31 {
 32   Random r(1,100);
 33   bool hit=r.get();
 34   if (hit_chance<=hit)
 35   {
 36       did_you_hit==false;
 37 
 38   }
 39   else
 40     did_you_hit==true;
 41 }
 42 
 43  class Knight{
 44    public:
 45         int stamina;
 46 
 47         bool attack();
 48         knight(string n, int stam);
 49         Weapon weapon_in_hand;
 50         bool on_horse(int exhausted);
 51         string name;
 52  };
 53 
 54 Knight:: knight(string n, int stam, &const Weapon)
 55 { 56 }
 57 
 58 bool Knight::on_horse
 59 {
 60   if (stam<=exhausted)
 61   {
 62     return false;
 63  }
 64 
 65   else
 66   return true;
 67 }
 68 
 69 bool Knight::attack(const Weapon)
 70 {
 71 
 72   {  get_stamina_required;
 73      stamina-=sr;
 74      did_you_hit;
 75 
 76   }
 77 
 78 
 79 }
 80 
 81 int main()
 82 {
 83   knight k1();
 84   cout << "What is the first knights name?" << endl;
 85   cin >> k1.name;
 86   cout << "What is knights stamina?" << endl;
 87   cin >> k1.stam;
 88   cout << "What is knights weapon?" << endl;
 89   cin >> k1.type;
 90   cout << "What is the weapon's hit chance?" << endl;
 91   cin>> k1.hc;
 92   cout << "What is the weapon's stamina required to use?" << endl;
 93   cin >> k1.sr;
 94 
 95   knight k2();
 96   cout << "What is the second knights name?" << endl;
 97   cin >> k2.nm;
 98   cout>> "What is" << name << "stamina?" << endl;
 99   cin >> k2.stam;
100   cout >> "What is knights weapon?" << endl;
101   cin>> k2.type;
102   cout << "What is weapon's hit chance?" << endl;
103   cin >> k2.hc;
104   cout << "What is weapon's stamina required?" << endl;
105   cin >> k2.sr;
106 
107 
108   while(k1.on_horse()==true && k1.exhausted()==false
109       &&
110         k2.on_horse()==true && k2.exhausted()==false)
   {
112     k1.attack;
113     if(did_you_hit==false)
114     {
115       k2.attack;
116     }
117     else
118       k2.on_horse==false;
119   }
                                                                                                                                                                   


When I try to compile it here is the error I get:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
projecttwo.cpp: In function 'bool did_you_hit()':
projecttwo.cpp:34: error: 'hit_chance' was not declared in this scope
projecttwo.cpp:40: error: ISO C++ forbids comparison between pointer and integer
projecttwo.cpp: At global scope:
projecttwo.cpp:48: error: ISO C++ forbids declaration of 'knight' with no type
projecttwo.cpp:54: error: expected identifier before '&' token
projecttwo.cpp:54: error: 'const' qualifiers cannot be applied to 'int&'
projecttwo.cpp:54: error: ISO C++ forbids declaration of 'knight' with no type
projecttwo.cpp:54: error: prototype for 'int Knight::knight(std::string, int, int&)' does not match any in class 'Knight'
projecttwo.cpp:48: error: candidate is: int Knight::knight(std::string, int)
projecttwo.cpp:58: error: 'bool Knight::on_horse' is not a static member of 'class Knight'
projecttwo.cpp:58: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
projecttwo.cpp:60: error: expected primary-expression before 'if'
projecttwo.cpp:60: error: expected '}' before 'if'
projecttwo.cpp:60: error: expected ',' or ';' before 'if'
projecttwo.cpp:65: error: expected unqualified-id before 'else'
projecttwo.cpp:67: error: expected declaration before '}' token
1
2
3
4
5
6
7
8
9
10
11
12
30   bool did_you_hit(void)
 31 {
 32   Random r(1,100);
 33   bool hit=r.get();
 34   if (hit_chance<=hit)
 35   {
 36       did_you_hit==false;
 37 
 38   }
 39   else
 40     did_you_hit==true;
 41 }


should be:

1
2
3
4
5
6
7
8
9
10
11
12
30   bool Weapon::did_you_hit(void)
 31 {
 32   Random r(1,100);
 33   bool hit=r.get();
 34   if (hit_chance<=hit)
 35   {
 36       did_you_hit==false;
 37 
 38   }
 39   else
 40     did_you_hit==true;
 41 }


the bool Knight::on_horse function is missing parentheses.

In your class declaration of Knight you define a constructor, ,but change the requirements when defining it.
I fixed my bool Weapon:: did_you_hit
added parentheses to on_horse
and added to my declaration of knight: Knight(string n, int stam, &const Weapon)

I still get this error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
projecttwo.cpp: In member function 'bool Weapon::did_you_hit()':
projecttwo.cpp:36: error: invalid use of member (did you forget the '&' ?)
projecttwo.cpp:40: error: invalid use of member (did you forget the '&' ?)
projecttwo.cpp: At global scope:
projecttwo.cpp:47: error: expected identifier before '&' token
projecttwo.cpp:47: error: 'const' qualifiers cannot be applied to 'int&'
projecttwo.cpp:47: error: ISO C++ forbids declaration of 'knight' with no type
projecttwo.cpp:53: error: expected identifier before '&' token
projecttwo.cpp:53: error: 'const' qualifiers cannot be applied to 'int&'
projecttwo.cpp:53: error: ISO C++ forbids declaration of 'knight' with no type
projecttwo.cpp: In member function 'bool Knight::on_horse(int)':
projecttwo.cpp:59: error: 'stam' was not declared in this scope
projecttwo.cpp: At global scope:
projecttwo.cpp:68: error: prototype for 'bool Knight::attack(Weapon)' does not match any in class 'Knight'
projecttwo.cpp:46: error: candidate is: bool Knight::attack()
projecttwo.cpp: In function 'int main()':
projecttwo.cpp:82: error: 'knight' was not declared in this scope
projecttwo.cpp:82: error: expected ';' before 'k1'
projecttwo.cpp:84: error: 'k1' was not declared in this scope
projecttwo.cpp:94: error: expected ';' before 'k2'
projecttwo.cpp:96: error: 'k2' was not declared in this scope
projecttwo.cpp:97: error: no match for 'operator>>' in 'std::cout >> "What is"'
projecttwo.cpp:97: error: 'name' was not declared in this scope
projecttwo.cpp:99: error: no match for 'operator>>' in 'std::cout >> "What is knights weapon?"'
projecttwo.cpp:112: error: 'did_you_hit' was not declared in this scope
projecttwo.cpp:118: error: expected '}' at end of input
did_you_hit is the name of your method. At lines 36 and 40, you appear to be trying to use it as the name of a data member, by assigning it a value. What you actually want to do there is to declare a local variable to contain the value you wish to return, and then return that value at the end of your method.

At line 48, you haven't declared a return type for the (empty) knight method. If it's not returning anything, it should have a void return type.

Is knight supposed to be a constructor for your Knight class? If so, you need to change it to use the correct case - C++ is a case-sensitive language.

Line 58 is incomplete - you need to specify the arguments for the method.

At line 60, you haven't declared stam, as stated in the compiler errors.

At line 69, the arguments for the method don't match those in the prototype in the class definition, as stated in the compiler errors.

etc, etc, etc

Last edited on
Topic archived. No new replies allowed.