error: forbids declaration with no type

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
 #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();
 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 Weapon:: did_you_hit()
 31 {
 32   Random r(1,100);
 33   int prob=r.get();
 34   if (hit_chance<=prob)
 35   {
 36       did_you_hit()==false;
 37   }
 38   else
 39     did_you_hit()==true;
 40 }
 41 
 42  class Knight{
 43    public:
 44         int stamina;
 45         bool attack();
 46         knight(string n, int stam, const Weapon);
 47         Weapon weapon_in_hand;
 48         bool on_horse();
49         string name;
 50  };
 51 
 52 Knight:: knight(string n, int stam, const Weapon)
 53 {
 54 }
 55 
 56 bool Knight::on_horse()
 57 {
 58   int exhausted=0;
 59   if (stam<=exhausted)
 60   {
 61     return false;
 62  }
 63 
 64   else
 65   return true;
 66 }
 67 
 68 bool Knight::attack(const Weapon)
 69 {
 70 
 71   {  get_stamina_required;
 72      stamina-=sr;
 73      did_you_hit;
 74 
 75   }
 76 
 77 
 78 }
 79 
 80 int main()
 81 {
 82   knight k1;
 83   cout << "What is the first knights name?" << endl;
 84   cin >> k1.name;
 85   cout << "What is knights stamina?" << endl;
 86   cin >> k1.stam;
 87   cout << "What is knights weapon?" << endl;
 88   cin >> k1.type;
 89   cout << "What is the weapon's hit chance?" << endl;
 90   cin>> k1.hc;
 91   cout << "What is the weapon's stamina required to use?" << endl;
 92   cin >> k1.sr;
 93 
 94   knight k2;
 95   cout << "What is the second knights name?" << endl;
 96   cin >> k2.nm;
 97   cout>> "What is" << name << "stamina?" << endl;
 98   cin >> k2.stam;
 99   cout >> "What is knights weapon?" << endl;
100   cin>> k2.type;
101   cout << "What is weapon's hit chance?" << endl;
102   cin >> k2.hc;
103   cout << "What is weapon's stamina required?" << endl;
104   cin >> k2.sr;
105 
106 
107   while(k1.on_horse()==true && k1.exhausted()==false
108       &&
109         k2.on_horse()==true && k2.exhausted()==false)
110   {
111     k1.attack();
112     if(did_you_hit==false)
113     {
114       k2.attack();
115     }
116     else
117       k2.on_horse==false;
118   }
                                                      


Getting this error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
projecttwo.cpp:46: error: ISO C++ forbids declaration of 'knight' with no type
projecttwo.cpp:52: error: ISO C++ forbids declaration of 'knight' with no type
projecttwo.cpp: In member function 'bool Knight::on_horse()':
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:45: 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


Not sure how to fix the first error , knight is my constructer that initializes data
ok first of all u have to have the constructor actually in the function and as a side note, its not actually initializing. its actually useless. the variables inside the constructor are only local to it btw. they dont have scope in anything else so thats why stam is wrong. actually u should retake the class tutorial. this code is written horribly. im going to revise it and show u what will make it look 10 x more efficient
@Aramil of Elixia
Okay! thank you!
in the mean time i would suggest taking a look at class and scope and constructor tutorials and i think my above post was a little harsh. i wasnt attacking ur abiltity to write good code
Topic archived. No new replies allowed.