Class within a Class / Class variable

Hey guys, I'm just learning about classes and I tried to make a class within a class. It didn't work and I've tried awhile but I cannot find the problem and I'm basically pulling my hair out at this point.

P.S. this isn't homework. Were only required to know how to make a class for the curriculum, not mesh them together. I want to learn how to mesh them though.

These are the four files I'm using, I haven't started the main source file with the main code yet:

Character.cpp
Character.h
Weapon.cpp
Weapon.h

I'll post them in order.


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
//  ****************************************************************************
//  Name: 
//  Date: 2/9/2012
//  COMSC 110 Computer Programming II
//  Assignment #: 
//  File Name: Character.cpp
//  Compiler Used:
//  ****************************************************************************

//  Compiler directives ********************************************************
#include <iostream>
#include "character.h"
#include "weapon.h"
using namespace std;



//  Class member function implementations **************************************
//
Character :: Character()
{
strength=0;
speed=0;
intelligence=0;
endurance=0;
awareness=0;
Weapon= Weapon();
}

Character :: Character(int cStrength, int cSpeed, int cIntelligence, int cEndurance, int cAwareness, Weapon Weapon)
{
strength=cStrength;
speed=cSpeed;
intelligence=cIntelligence;
endurance=cEndurance;
awareness=cAwareness;
Weapon = Gun.Weapon();
}

void Character :: read()
{
       cout << "Please enter character strength: ";
       cin >> strength;
       cout << endl;
       
       cout << "Please enter character speed: ";
       cin >> speed;
       cout << endl;
       
       cout << "Please enter character intelligence: ";
       cin >> intelligence;
       cout << endl;
       
       cout << "Please enter character endurance: ";
       cin >> endurance;
       cout << endl;
       
       cout << "Please enter character awareness: ";
       cin >> awareness;
       cout << endl;
       
       cout << "Please enter character's weapon: ";
       cin >> weapon;
       cout << endl;
}

void Character :: print() const
{
     cout << "Strength is: " << strength << endl;
     cout << "Speed is: " << speed << endl;
     cout << "intelligence is: " << intelligence << endl;
     cout << "Endurance is: " << endurance << endl;
     cout << "Awareness is: " << awareness << endl;
     cout << "Weapon is: " << weapon.print() << endl;
}
     

// Non-member function implementations *****************************************


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
//  ****************************************************************************
//  Name: Cory Paisley
//  Date: 2/9/2012
//  COMSC 110 Computer Programming II
//  Assignment #: 
//  File Name: Character.h
//  Compiler Used:
//  ****************************************************************************

//  Compiler directives
#ifndef CHARACTER_H
#define CHARACTER_H

#include <iostream>
#include <string>
#include "weapon.h"
using namespace std;

// Class definitions and function prototypes go here



class Character
{
 public:
        Character();
        Character(int cStrength, int cSpeed, int cIntelligence, int cEndurance, int cAwareness, Weapon Weapon);
        void read();
        void print() const;  
 private:
        string name;
        int strength;
        int speed;
        int intelligence;
        int endurance;
        int awareness;
        Weapon Weapon;
        int inventory[10];
 };    
       


// end of the compiler directives
#endif




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
//  ****************************************************************************
//  Name:
//  Date:
//  COMSC 110 Computer Programming II
//  Assignment #: 
//  File Name: Weapon.cpp
//  Compiler Used:
//  ****************************************************************************

//  Compiler directives ********************************************************
#include <iostream>
#include "weapon.h"
using namespace std;



//  Class member function implementations **************************************
//
Weapon :: Weapon()
{
       damage=0;
       accuracy=0;
       clipSize=0;
       totalAmmo=0;
}

Weapon :: Weapon(int wDamage, int wAccuracy, int wClipSize, int wTotalAmmo)
{
       damage= wDamage;
       accuracy= wAccuracy;
       clipSize= wClipSize;
       totalAmmo= wTotalAmmo;
}

void Weapon :: read()
{
       cout << "Please enter weapon damage: ";
       cin >> damage;
       cout << endl;
       
       cout << "Please enter weapon accuracy: ";
       cin >> accuracy;
       cout << endl;
       
       cout << "Please enter weapon clip size: ";
       cin >> clipSize;
       cout << endl;
       
       cout << "Please enter weapon's total ammo: ";
       cin >> totalAmmo;
       cout << endl;
}

void Weapon :: print() const
{
     cout << "Weapon Damage is: " << damage << endl;
     cout << "Weapon Accuracy is: " << accuracy << endl;
     cout << "Weapon clipsize is: " << clipSize << endl;
     cout << "Weapon max ammo is: " << totalAmmo << endl;
}
        

// Non-member function implementations *****************************************


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
//  ****************************************************************************
//  Name: Cory Paisley
//  Date: 2/9/2012
//  COMSC 110 Computer Programming II
//  Assignment #: 
//  File Name: Weapon.h
//  Compiler Used:
//  ****************************************************************************

//  Compiler directives
#ifndef WEAPON_H
#define WEAPON_H

#include <iostream>
#include <string>

using namespace std;

// Class definitions and function prototypes go here
class Weapon
{
 public:
        Weapon();
        Weapon(int wDamage, int wAccuracy, int wClipSize, int wTotalAmmo);
        void read();
        void print() const;  
 private:
        string name;
        int damage;
        int accuracy;
        int clipSize;
        int totalAmmo;
 }; 
    
        


// end of the compiler directives
#endif





Character isn't working, because weapon isn't recognized as a class. my error code message is


12 E:\Forum Games\Dead Space\Character.cpp In file included from Character.cpp

37 E:\Forum Games\Dead Space\character.h declaration of `Weapon Character::Weapon'

21 E:\Forum Games\Dead Space\weapon.h changes meaning of `Weapon' from `class Weapon'

27 E:\Forum Games\Dead Space\Character.cpp no match for call to `(Weapon) ()'

30 E:\Forum Games\Dead Space\Character.cpp `Weapon' is not a type

Thanks for any advice you guys can give
Last edited on
You declare a Weapon with the name Weapon in Character.cpp. Try changing name to lowercase
1
2
3
4
5
6
7
class Character
{
 public:
       //  .... 
       Weapon Weapon;  //  <--- Here, change to lowercase
        int inventory[10];
 };  


That is just like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Weapon
{
  private:
    int damage;
};

class Character
{
  private:
    // Bad
    Weapon Weapon; 
    // When changed to lowercase, No error!
    // Weapon weapon;
};

int main()
{
   return 0;
}
$ g++ a.cpp
a.cpp:12: error: declaration of ‘Weapon Character::Weapon’
a.cpp:4: error: changes meaning of ‘Weapon’ from ‘class Weapon’
$

Last edited on
Thanks, Hist that got rid of some of the errors I think I may can figure it out from here I appreciate it.
Topic archived. No new replies allowed.