Why isn't this working?!

I'm doing an assignment for a game program I'm writing. I have three files:
Weapon.h
Weapon.cpp
and assignment10.cpp

I've put them altogether and made a makefile, but its giving me error messages about defining. here are my three files.

Weapon.h
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 


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


and

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


constructors arent functions they go in the class and they dont have prototypes
I thought my constructer was in the Weapons class in Weapon.h ??
constructors can have prototypes and can be defined anywhere... they can be defined inside the classes too..
Last edited on
It could be as simple as the fact that you forgot to #include "Weapon.h" in assignment10.cpp. Seriously.
Last edited on
I was told to only #include Weapon.h once, which I did in Weapon.cpp
Once per cpp file. Once you understand how *.h and *.cpp files are actually used, you'll understand why. I will now explain enough for you to understand it and know for yourself when to include header files.

Here is what happens when you build an executable or library made of more than one cpp file.

Each cpp file is examined by the preprocessor. If it finds any #include somefile , the complete contents of the file somefile is copied into the cpp file at that point.

Then, the compiler takes each cpp file (which now has the contents of various header files written into it, just as if you had cut and paste it there yourself with your text editor) and compiles it into a binary object file. Each cpp file is dealt with individually, so each cpp file must include any headers it needs itself.

When all the cpp files have been compiled into binary object files, the linker gets to work. All those object files are linked together. If you're making a library, that's pretty much it. If you're making an executable, the linker links them together with some system specific objects and presents the output executable (the system specific bits take care of setting things up when you run the executable, and there will be something in there that calls your main function). If an object file contains an identical function definiton to that in another object file, the linker will not know which one to use when you call the function - this is what goes wrong when you put the same function definition in more than one cpp file.

How does the compiler know which cpp files to compile? Because the compiler is a command-line tool, and when you call it, you call it with the names of the files to compile:

g++ file1.cpp file2.cpp file3.cpp

If you're using an IDE, it may hide this from you. It's still doing it.

How does the linker know which object files to link? Because the linker is a command-line tool, and when you call it, you tell it which object files to link, much like the compiler. Again, if you're using an IDE, it may hide this from you. It's still doing it.

Last edited on
After fixing that issue I still get this error when trying to compile it using the command "make":
1
2
3
4
5
6
7
make: Warning: File `assignment10.cpp' has modification time 1.1e+03 s in the future
g++ -o assignment10 assignment10.cpp Weapon.cpp
Weapon.cpp: In function 'Weapon w1(std::string, int, int)':
Weapon.cpp:13: error: 'weapon_type' was not declared in this scope
Weapon.cpp:14: error: 'stamina_required' was not declared in this scope
Weapon.cpp:15: error: 'hit_chance' was not declared in this scope
make: *** [assignment] Error 1 
all of ur variables arent in the class scope try writing the functions inline
@computermajor12

1
2
3
public:
         void display(void);
       Weapon(string weapon_type, int hit_chance, int stamina_required); 


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

This is not related to your problem, and doesn't cause an error - luckily. You should get into the habit of making sure that function declarations; definitions; and calls, all have their parameters / arguments in the same order.

@Aramil of Elixia

constructors arent functions they go in the class and they dont have prototypes


all of ur variables arent in the class scope try writing the functions inline


The first one is completely wrong. Constructors are functions, they can be declared, defined and overloaded just like any other function.

With the second comment, writing the functions inline should not have to be a solution to this. There is some other problem, but I can't see it.

Last edited on
In function 'Weapon w1(std::string, int, int)':
¿? Show your update code.

Also, you should include what you use. Add #include <string> and get rid of #include <iostream> in Weapon.h (it should be in Weapon.cpp though)
Topic archived. No new replies allowed.