Problem giving an object another object / joust game

I am writing a jousting game for my 111 class and am having trouble figuring out why my Knight object/class wont recognize my weapon object/class.

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
    //knight.h
    #include <iostream>
    #include <string>
                #include <cstdlib>
                
                using namespace std;
                
                class Knight
                {
                  private: 
                    string m_name;
                    int m_stamina;
                    bool m_onHorse;
                    Weapon w;
                
                  public:
                    Knight(string, int, string, int, int);
                    bool onHorse();
                };
                
//knight.cpp
                
                #include "knight.h"
                #include "weapon.h"
                Knight::Knight(string name, int stamina, string wepName, int missChance, int stamUse)
                  : m_name(name), m_stamina(stamina), w(wepName, missChance, stamUse)
                {
                }
                
                bool Knight :: onHorse()
                {
                  if (m_stamina > 0)
                  {
                    m_onHorse = true;
                  }
                  else 
                  {
                    m_onHorse = false;
                  }
                }   
                
//weapon.h
            
            #include <iostream>
            #include <string>
            using namespace std;
            class Weapon
            {
              private:
                int stamUse;
                int missChance;
                string wepName;
              public:
                Weapon(string, int, int);
            };
//weapon.cpp
            #include "weapon.h"
            
              Weapon::Weapon(string wepName, int stamUse, int missChance)
            :wepName(wepName), stamUse(stamUse), missChance(missChance)
            {
            }
            
//main.cpp
            #include "Random.h"
            #include "knight.h"
            #include "weapon.h"
            #include <unistd.h>
            int main()
            {
              Knight userKnight(string name, int stamina, string wepName, int missChance, int stamUse);
              cout << "What would you like your knight's name to be?" << endl;
              getline(cin, name);
              cout << name << endl;
            return 0;
            }
            
            
Also there is a random.cpp and random.h file provided by my teacher, but I havent gotten errors using those yet. Errors I am getting are:
        
        g++ Random.cpp knight.cpp weapon.cpp main.cpp -o output
        In file included from knight.cpp:1:0:
        knight.h:14:5: error: 'Weapon' does not name a type
             Weapon* w;
             ^
        knight.cpp: In constructor 'Knight::Knight(std::string, int, std::string, int, i
        knight.cpp:4:39: error: class 'Knight' does not have any field named 'w'
           : m_name(name), m_stamina(stamina), w(wepName, missChance, stamUse)
                                               ^
        In file included from main.cpp:2:0:
        knight.h:14:5: error: 'Weapon' does not name a type
             Weapon w;
             ^
        main.cpp: In function 'int main()':
        main.cpp:9:16: error: 'name' was not declared in this scope
           getline(cin, name);
    
Im wondering why my knight class is not recognizing the weapon object given to it in knight.h please help

 
Last edited on
Move #include "weapon.h" from knight.cpp to knight.h. You might also want to think about using header guards.
Awesome that worked and I got rid of almost everything, but I still get:
main.cpp: In function 'int main()':
main.cpp:9:16: error: 'name' was not declared in this scope
getline(cin, name);

Anyone know why by chance, do I need to set a separate string in main to the string in my knight constructor?
At line 71 of your OP, you have:

Knight userKnight(string name, int stamina, string wepName, int missChance, int stamUse);
This is declaring a function called userKnight, which takes 5 arguments.

I suspect that what you wanted to do was declare 5 variables, initialise them with values, and then pass those values into the constructor for Knight to instantiate an object called userKnight.
Topic archived. No new replies allowed.