help with simple program

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 and a constructor (which takes 3 arguments to initialize the private data). 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
 #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 


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


I can't get this code to compile and I've been working on it for a very long time. Any idea's on whats wrong with my code?
Help please!
What errors are you getting ?

If you actually have the number lines in the code then it definitely wont compile

EDIT:
also you arent giving a value to weapon
 
Weapon:: Weapon(string i_weapon, int stamina, int hit):hit_chance(hit), stamina_required(stamina), weapon(i_weapon) {} 

Should fix it
Last edited on
I did what you said, still getting these errors:
when I compile Weapon.cpp

1
2
3
4
Weapon.cpp: In member function 'void Weapon::display()':
Weapon.cpp:12: error: 'hit' was not declared in this scope
Weapon.cpp:13: error: 'stamina' was not declared in this scope
 


When I try to compile assignment10.cpp:
1
2
3
4
/tmp/ccRiQHkt.o: In function `main':
assignment10.cpp:(.text+0x45): undefined reference to `Weapon::Weapon(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
assignment10.cpp:(.text+0x84): undefined reference to `Weapon::display()'
collect2: ld returned 1 exit status 
Replace your display function with this
1
2
3
4
5
6
void Weapon::display(void)
  {
    cout << "hit chance=" << hit_chance  << endl;
    cout << "stamina required=" << stamina_required  << endl;
    cout << "weapon type is" << weapon << endl;
  }


You got confused with the constructor input with the member names of the class if that makes any sense

I'm still getting two errors:
1
2
3
/usr/lib64/gcc/x86_64-slackware-linux/4.4.4/../../../../lib64/crt1.o: In function `_start':
/glibc-tmp-4211a76efa4c5de6d46269f47808acd6/glibc-2.11.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
collect2: ld returned 1 exit status


1
2
3
4
/tmp/cc2w47FN.o: In function `main':
assignment10.cpp:(.text+0x45): undefined reference to `Weapon::Weapon(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int)'
assignment10.cpp:(.text+0x84): undefined reference to `Weapon::display()'
collect2: ld returned 1 exit status 
Honestly i have no idea why you are getting that
I just compiled and ran it and it seems fine

In your weapon header put all the include you need
1
2
3
4
#include <iostream>
#include <string>

using namespace std;


then remove all other iostream, string and using namespace std lines from other files

Then if you still have an error post it and all the code
Topic archived. No new replies allowed.