Error in code--need help fixing

I'm doing a part to a project where I'm making a weapon class and a test program. The weapon class has to have three private data members: two integers (hit_chance and stamina_required) and a string for the weapon type. There is suppossed to be two public member functions: a display function which prints out the private data members. My teacher wants us to split the Weapon class into Weapon.h and Weapon.cpp and to put the main() function in another file named assignment10.cpp
Then I'm writing a makefile to build the program.
Heres what I have for the Weapon.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  1 #include<string>
  2 using namespace std;
  3 
  4 
  5 class Weapon {
  6     private:
  7              int hit_chance;
  8              int stamina_required;
  9              string weapon;
 10 
 11 
 12     public:
 13           void display(void);
 14           Weapon(string weapon, int hit, int stamina);
 15 
 16 };
 17 
~                 


Weapon.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  1 #include "Weapon.h"
  2 #include<iostream>
  3 #include<string>
  4 using namespace std;
  5 
  6 Weapon:: Weapon(string weapon, int stamina, int hit)
  7 {
  8   stamina_required=stamina;
  9   hit_chance=hit;
 10 }
 11 void Weapon::display(void)
 12 {
 13   cout << "hit chance=" << hit  << endl;
 14   cout << "stamina required=" << stamina  << endl;
 15   cout << "weapon type is" << weapon << endl;
 16 }
 17 


assignment10.cpp:
1
2
3
4
5
6
7
8
9
10
11
 1 using namespace std;
  2 #include"Weapon.h"
  3       
  4 int main()
  5       
  6 {    
  7  Weapon w1; 
  8   w1.weapon ("Lance", 13, 5);
  9   w1.display(); 
 10   return 0;
 11  }


I can't get these to compile correctly and Ive been working on them for a really long time. Any idea's on what the errors in my code are and how I can fix them?

Your class has no the default constructor. It has constructor with three parameters.

Weapon(string weapon, int hit, int stamina);

So you can not create an object of the class such way

Weapon w1;
1
2
Weapon w1; 
w1.weapon ("Lance", 13, 5);
That's not how you call a constructor. Do it like this instead: Weapon w1("Lance", 13, 5);
okay so I change this code to fixed code:

1
2
3
4
5
6
7
8
9
10
11
12
 1 using namespace std;
  2 #include"Weapon.h"
  3       
  4 int main()
  5       
  6 {    
  7     
  8  Weapon w1 ("Lance", 13, 5);
  9   w1.display(); 
 10   return 0;
 11  }
~        


but it still won't compile!
Last edited on
What error do you get?

When you post code it's better if you don't include the line numbers. It would make it easier for everyone that want to compile the code.
Topic archived. No new replies allowed.