Help with assignment !!!

Here's my assignment:
For this assignment you will write a simple Weapon class and a test program.
The Weapon class should have three private data members: two integers (hit_chance and stamina_required) and a string for the weapon type.
There should be two public member functions: a constructor (which takes 3 arguments to initialize the private data) and a display function which prints out the private data members.
Split the Weapon class into Weapon.h and Weapon.cpp.
Put the main() function in a file named: assignment10.cpp
Write a Makefile to build the application.

My Weapon.h class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  1 #include<iostream>
  2 using namespace std;
  3 
  4 class Weapon {
  5     private:
  6              int hit_chance;
  7              int stamina_required;
  8              string weapon_type;
  9 
 10     public:
 11           void display(void);
 12           Weapon(string weapon_type, int hit_chance, int stamina_required);
 13 
 14 
 15 }
 16 


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


and my assignment10.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  1 #include<iostream>
  2 #include"Weapon.h"
  3 #include"Weapon.cpp"  
  4      
  5 using namespace std;
  6      
  7     
  8 int main()
  9  {       
 10   Weapon w1("Lance", 13, 5);
 11   w1.display();
 12   return 0;
 13  } 
~                                                                               
~                                                                               
~            


I keep getting error about my #include"Weapon.h"
and I'm not sure why, also it keeps saying my statements are undeclared or not defined. Am i spliting up my class into Weapon.h and Weapon.cpp? (Thats what my teacher asked).. Any help with this would be great!
Don't #include source (.cpp) files. Those should be added to your project, not textually included into a file.
You include iostream in all three of your files. When you include weapon.h inside of weapon.cpp the compiler is being told to include iostream twice, and then when you include weapon.h and weapon.cpp inside of assignment10.cpp the compiler is being told to add iostream to assignment10.cpp 5 times.

Similarly because weapon.h is inside of weapon.cpp when you include weapon.h and weapon.cpp inside of assignment10.cpp its like you are adding weapon.h to assignment10.cpp twice.

To clarify, when you add a header to a file, it's similar to copy pasting the entire header contents to the top of that file. This means that when you include a header file into a second file, every header file included into THAT header file is also included.

A few other things. Weapon.h includes the class prototype for weapon.cpp. This means you only need to include weapon.h into assignment10.cpp (weapon.h is still going to be included inside of weapon.cpp, it's just that weapon.cpp doesn't need to be included in assignment10.cpp). Also you need a semicolon at the end of line 15 of weapon.h because it's a class declaration not a definition.

TLDR:
1. Remove #include <iostream> from assignment10.cpp and weapon.cpp.
2. Remove weapon.cpp from assignment10.cpp.
3. Add a semicolon to line 15 of weapon.h (outside the brackets)

Alternatively,
Look up the #ifndef, #define, and #endif commands to help you with header problems in the future.
Last edited on
I fixed all those issues and it still isn't working
I fixed all those issues and it still isn't working


Perhaps you could be a little more specific.
try adding semi colon ';' at the end of the weapon class declaration
Topic archived. No new replies allowed.