error- (did you forget '&')

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
[code]#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   }
                                                                                                                                                                   
[/code]

This is my code and I'm getting this error:
can't figure out the first error in line 36, how could I fix this error 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
projecttwo.cpp: In member function 'bool Weapon::did_you_hit()':
projecttwo.cpp:36: error: invalid use of member (did you forget the '&' ?)
projecttwo.cpp:39: error: invalid use of member (did you forget the '&' ?)
projecttwo.cpp: At global scope:
projecttwo.cpp:46: error: expected identifier before '&' token
projecttwo.cpp:46: error: 'const' qualifiers cannot be applied to 'int&'
projecttwo.cpp:46: error: ISO C++ forbids declaration of 'knight' with no type
projecttwo.cpp:52: error: expected identifier before '&' token
projecttwo.cpp:52: error: 'const' qualifiers cannot be applied to 'int&'
projecttwo.cpp:52: error: ISO C++ forbids declaration of 'knight' with no type
projecttwo.cpp: In member function 'bool Knight::on_horse(int)':
projecttwo.cpp:58: error: 'stam' was not declared in this scope
projecttwo.cpp: At global scope:
projecttwo.cpp:67: error: prototype for 'bool Knight::attack(Weapon)' does not match any in class 'Knight'
projecttwo.cpp:45: error: candidate is: bool Knight::attack()
projecttwo.cpp: In function 'int main()':
projecttwo.cpp:81: error: 'knight' was not declared in this scope
projecttwo.cpp:81: error: expected ';' before 'k1'
projecttwo.cpp:83: error: 'k1' was not declared in this scope
projecttwo.cpp:93: error: expected ';' before 'k2'
projecttwo.cpp:95: error: 'k2' was not declared in this scope
projecttwo.cpp:96: error: no match for 'operator>>' in 'std::cout >> "What is"'
projecttwo.cpp:96: error: 'name' was not declared in this scope
projecttwo.cpp:98: error: no match for 'operator>>' in 'std::cout >> "What is knights weapon?"'
projecttwo.cpp:111: error: 'did_you_hit' was not declared in this scope
projecttwo.cpp:117: error: expected '}' at end of input
Last edited on
In C++ when you want a function to evaluate to a value, you return that value. You do not assign the value to the function name.


Wrong:
1
2
3
4
5
6
7
8
9
10
11
12
bool did_you_hit(void)
{
  Random r(1,100);
  bool hit=r.get();
  if (hit_chance<=hit)
  {
      did_you_hit==false;

  }
  else
    did_you_hit==true;
}


right:
1
2
3
4
5
6
7
8
9
10
11
bool did_you_hit()
{
  Random r(1,100);
  bool hit=r.get();
  if (hit_chance<=hit)
  {
      return false;
  }
  else
    return true;
}


or better yet:
1
2
3
4
5
6
bool did_you_hit()
{
  Random r(1,100);
  bool hit=r.get();
  return (hit_chance > hit);
}



EDIT:

There are some other syntax errors as well. On line 54 you probably meant const Weapon& not &const Weapon.

Line 58 is missing parenthesis.


Also, Lines 83 and 92 are wrong. You are prototyping functions by mistake:

1
2
3
knight k1(); // <- not what you want.  Declares a function that returns a knight

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
projecttwo.cpp:18: error: expected ')' before 'type'
projecttwo.cpp: In constructor 'Knight::Knight(std::string, int, Weapon)':
projecttwo.cpp:62: error: 'n' was not declared in this scope
projecttwo.cpp:62: error: no matching function for call to 'Weapon::Weapon()'
projecttwo.cpp:13: note: candidates are: Weapon::Weapon(std::string, int, int)
projecttwo.cpp:6: note:                 Weapon::Weapon(const Weapon&)
projecttwo.cpp: In member function 'bool Knight::on_horse()':
projecttwo.cpp:69: error: 'stam' was not declared in this scope
projecttwo.cpp: At global scope:
projecttwo.cpp:78: error: prototype for 'bool Knight::attack(Weapon)' does not match any in class 'Knight'
projecttwo.cpp:55: error: candidate is: bool Knight::attack()
projecttwo.cpp: In function 'int main()':
projecttwo.cpp:92: error: 'knight' was not declared in this scope
projecttwo.cpp:92: error: expected ';' before 'k1'
projecttwo.cpp:94: error: 'k1' was not declared in this scope
projecttwo.cpp:104: error: expected ';' before 'k2'
projecttwo.cpp:106: error: 'k2' was not declared in this scope
projecttwo.cpp:107: error: no match for 'operator>>' in 'std::cout >> "What is"'
projecttwo.cpp:107: error: 'name' was not declared in this scope
projecttwo.cpp:109: error: no match for 'operator>>' in 'std::cout >> "What is knights weapon?"'
projecttwo.cpp:122: error: 'did_you_hit' was not declared in this scope
projecttwo.cpp:128: error: expected '}' at end of input
These are really simple errors. I think you should go read a few c++ tutorials before continuing
Line 18 should be
 
 18 Weapon(string type, int sr, int hc)
62 should be
 
   :name(name), stamina(stam)
also you dont even use the 3rd parameter... Theres just to many errors X)
Last edited on
Topic archived. No new replies allowed.