Declaring Arrays in Nested Classes

Hello there,
I am a student, and am pretty new to C++. I recently began developing a cricket simulator game using Dev-C++ (for those who are unaware, cricket is a team sport popular in the Indian subcontinent, Australia & England, and is a highly statistical game, involving immense data and calculations, thereby making my game very complex using simple algorithms since I am a novice).
I wanted to create my team players in such a way that I use nested classes to refer to the Team, which is an array, and the player, which is also defined in an array. However, I am unable to declare both class members as arrays. Here is my code
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream.h>
#include <string>

class Team
{
      public:
      std::string Name;
      std::string Location;
      std::string GoverningBody;
      std::string FirstTestMatch;
      std::string FirstODIMatch;
      std::string FirstT20Match;
      std::string PresidentOfGovBody;
      std::string TestCaptain;
      std::string ODICaptain;   
      std::string T20Captain;
      
      public: class ODITeam
      {
      public:
      std::string Name;
      std::string DateOfBirth;
      std::string MajorTeams;
      std::string BattingStyle;
      std::string BowlingStyle;
      unsigned int ODIMatchesPlayed;
      unsigned int ODIInnings;
      unsigned int ODIRunsScored;
      double ODIBattingAvg;
      unsigned int ODIHighScore;
      unsigned int ODIHundreds;
      unsigned int ODIFifties;
      double ODISR;
      unsigned int ODICatches;
      unsigned int ODIWicketsTaken;
      double ODIBowlingAvg;
      double ODIBowlingSR;
      double ODIBowlingER;
      };
};


int main()
{
     system("color 17");
     

     Team Team[8];
     Team[1].Name = "India";
     Team[1].Location = "Indian Subcontinent";
     Team[1].GoverningBody = "BCCI";
     Team[1].PresidentOfGovBody = "Shashank Manohar";
     Team[1].FirstTestMatch = "June 25, 1932 vs England";
     Team[1].FirstODIMatch = "July 13, 1974 vs England";
     Team[1].FirstT20Match = "December 1, 2007 vs South Africa";
     Team[1].TestCaptain = "MS Dhoni";
     Team[1].ODICaptain = "MS Dhoni";
     Team[1].T20Captain = "MS Dhoni";
     
     ODITeam ODIPlayer[11];
     // Virender Sehwag - Player 1
     ODIPlayer[1].Name = "Virender Sehwag "; 
     ODIPlayer[1].DateOfBirth = "October 20, 1978";
     ODIPlayer[1].MajorTeams = "India, Asia XI, Delhi Daredevils, Delhi, ICC World XI";
     ODIPlayer[1].BattingStyle = "Right-hand Opener";
     ODIPlayer[1].BowlingStyle = "Right-arm Offbreak";
     ODIPlayer[1].ODIMatchesPlayed = 236;
     ODIPlayer[1].ODIInnings = 230;
     ODIPlayer[1].ODIRunsScored = 7760;
     ODIPlayer[1].ODIBattingAvg = 35.1;
     ODIPlayer[1].ODIHighScore = 175;
     ODIPlayer[1].ODIHundreds = 14;
     ODIPlayer[1].ODIFifties = 37;
     ODIPlayer[1].ODISR = 104.1;
     ODIPlayer[1].ODICatches = 87;
     ODIPlayer[1].ODIWicketsTaken = 92;
     ODIPlayer[1].ODIBowlingAvg = 42.4;
     ODIPlayer[1].ODIBowlingSR = 45.9;
     ODIPlayer[1].ODIBowlingER = 5.3;
     

     // Sachin Tendulkar - Player 2
     ODIPlayer[2].Name = "Sachin Tendulkar"; 
     ODIPlayer[2].DateOfBirth = "April 24, 1973";
     ODIPlayer[2].MajorTeams = "India, Asia XI, Mumbai, Mumbai Indians, Yorkshire";
     ODIPlayer[2].BattingStyle = "Riht-hand Bat; Test - Middle Order; ODI - Opening";
     ODIPlayer[2].BowlingStyle = "Right-arm Offbreak, Legbreak Googly";
     ODIPlayer[2].ODIMatchesPlayed = 453;
     ODIPlayer[2].ODIInnings = 442;
     ODIPlayer[2].ODIRunsScored = 18111;
     ODIPlayer[2].ODIBattingAvg = 45.2;
     ODIPlayer[2].ODIHighScore = 200;
     ODIPlayer[2].ODIHundreds = 48;
     ODIPlayer[2].ODIFifties = 95;
     ODIPlayer[2].ODISR = 86.3;
     ODIPlayer[2].ODICatches = 136;     
     ODIPlayer[2].ODIWicketsTaken = 154;
     ODIPlayer[2].ODIBowlingAvg = 44.4;
     ODIPlayer[2].ODIBowlingSR = 52.2;
     ODIPlayer[2].ODIBowlingER = 5.1;

     // Gautam Gambhir - Player 3
     ODIPlayer[3].Name = "Gautam Gambhir  "; 
     ODIPlayer[3].DateOfBirth = "October 14, 1981";
     ODIPlayer[3].MajorTeams = "India, Kolkata Knight Riders, Delhi, India Red";
     ODIPlayer[3].BattingStyle = "Left-hand Opener";
     ODIPlayer[3].BowlingStyle = "Legbreak";
     ODIPlayer[3].ODIMatchesPlayed = 114;
     ODIPlayer[3].ODIInnings = 110;
     ODIPlayer[3].ODIRunsScored = 4073;
     ODIPlayer[3].ODIBattingAvg = 40.7;
     ODIPlayer[3].ODIHighScore = 150;
     ODIPlayer[3].ODIHundreds = 9;
     ODIPlayer[3].ODIFifties = 25;
     ODIPlayer[3].ODISR = 86.4;
     ODIPlayer[3].ODICatches = 32;   
     ODIPlayer[3].ODIWicketsTaken = 0;
     ODIPlayer[3].ODIBowlingAvg = 0;
     ODIPlayer[3].ODIBowlingSR = 0;
     ODIPlayer[3].ODIBowlingER = 13;     
     
     // Virat Kohli - Player 4
     ODIPlayer[4].Name = "Virat Kohli     "; 
     ODIPlayer[4].DateOfBirth = "November 5, 1988";
     ODIPlayer[4].MajorTeams = "India, India U-19, Delhi, Royal CHallengers Bangalore";
     ODIPlayer[4].BattingStyle = "Right-hand Middle Order";
     ODIPlayer[4].BowlingStyle = "Right-arm Medium";
     ODIPlayer[4].ODIMatchesPlayed = 59;
     ODIPlayer[4].ODIInnings = 56;
     ODIPlayer[4].ODIRunsScored = 2153;
     ODIPlayer[4].ODIBattingAvg = 43.9;
     ODIPlayer[4].ODIHighScore = 118;
     ODIPlayer[4].ODIHundreds = 5;
     ODIPlayer[4].ODIFifties = 15;
     ODIPlayer[4].ODISR = 81.9;
     ODIPlayer[4].ODICatches = 27;   
     ODIPlayer[4].ODIWicketsTaken = 0;
     ODIPlayer[4].ODIBowlingAvg = 0;
     ODIPlayer[4].ODIBowlingSR = 0;
     ODIPlayer[4].ODIBowlingER = 5.8;
     
         
       
     cin.ignore();
     return 0;
}
     

When I compile this code, it says "ODIPlaer undeclared (first use this function). I think the problem is with Lines 48 and 60. Can you help me so that if given values of integers x and y, it can print out Team[x].ODIPlayer[y].Name ??? Could it have to do with functions or methods I have to declare within the classes ??? Any help is greatly appreciated. And please make your explanations very simple and very clear if the solution is very complicated.

Regards,
Tejas
Last edited on
ODIPlayer is declared inside the class Team.....so it cannot be accessed directly....
Dang it. Late.

Your ODITeam is declared inside of another class, so when it comes time for you to create your array with player information, the class is not in scope.

May I suggest creating your array inside the class? Oh, and it is *not* a good idea to use variable names that match your type's names.

Finally, we'd suggest moving away from Dev-C++ and using something else for C++ programming.

-Albatross
As your nested class definition is public, your code should compile if you use

Team::ODITeam ODIPlayer[11];

But this doesn't make a lot of sense from an OO design point of view.

Andy

Thanks a lot for the help. Will try and see if your advice helps me. Thanks a lot once more.
I am still having a problem here .... when I try to print the following code, it gives an error saying "class Team has no member named ODIPlayer" ...
1
2
3
4
5
6
7
 int x;
     int y;
     y = 1;
     for(x = 1; x <= 9; x++)
     {                
           cout << Team[1].ODIPlayer[x].Name << " - \t" << Team[1].ODIPlayer[x].MajorTeams << endl;
     }


Can somebody please help me overcome this ?
Given the class definitions you posted earlier, your players are declared as a global array, not a class member.

So, if you still have

ODITeam ODIPlayer[11];

declared in main, then you only have a single array of players. So the following will work

cout << ODIPlayer[x].Name << ...

but not

cout << Team[1].ODIPlayer[x].Name << ...


If you want to define players for each team, you need to make ODIPlayer a member of the class.

Andy
Last edited on
Yeah I figured that

cout << ODIPlayer[x].Name << ...

works, but I wanted to put in the form of

cout << Team[1].ODIPlayer[x].Name << ...

for simplicity of code.

You mentioned making ODIPlayer a member of the class. How about multidimensional arrays? Then if the team number is 3 and the player no. is 7 then I can declare it as

cout << ODIPlayer[3][7].Name << ...

Thanks for the help Andy. Really appreciate it.
Last edited on
Well, a multi-dimensional array would allow you to handle all the teams players, but it's not good from an OO design point of view. I think it's prob. slightly worse than the array member approach.

Have you covered OO design yet? And come across the difference between association and aggregation?

Andy

P.S. Coincidentally, I once had an interview question about a very similar problem. I had to sketch out the design for a football (as in soccer) game...
Topic archived. No new replies allowed.