How to properly call a vector from a class?

Hello! I am making a Pokemon Battle Simulator in Visual Studio 2017, where I am having troubles on lines 198 - 205. I am trying to call a vector from the class "Attack", which is inherited by the class "Pokemon", but I am having trouble. How would I properly call this vector?

Here is the 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int critical;


class Attack
{
public:
	//constructor for attack taking all the information
	Attack(string atkName, int atkPower, int recoil, int atkPP, int atkAcc, string atkType) : //this is used post c++ 11 to initialize the member variable and behaves identical to 
		name(atkName),//name == atkName
		Recoil(recoil),
		power(atkPower), //power == atkPower
		pp(atkPP),
		accuracy(atkAcc),
		type(atkType)
	{
	}

	//accessor methods. Returns the value of the member variable. 
	string getName()
	{
		return name;
	}

	//if you need a public way to change a member variable create the setter
	//void setName(string newName)
	//{
	//  name  = newName;
	//}
	int getPower()
	{
		return power;
	}

	int getRecoil()
	{
		return Recoil;
	}

	int getPP()
	{
		return pp;
	}

	int getAccuray()
	{
		return accuracy;
	}

	string getType()
	{
		return type;
	}

private:
	string name;
	int power;
	int Recoil;
	int pp;
	int accuracy;
	string type;
};

class Pokemon
{
public:
	//constructor with all the needed information to create a pokemon
	Pokemon(string pokeName, string type1, string type2, int HPstat, int ATKstat, int SpATKstat, int DEFstat, int SpDEFstat, int Speedstat, vector<Attack> &attacks) :
		name(pokeName),
		type1(type1),
		type2(type2),
		HP(HPstat),
		Atk(ATKstat),
		SpAtk(SpATKstat),
		Def(DEFstat),
		SpDEF(SpDEFstat),
		Speed(Speedstat),
		attackList(attacks)
	{}

	//prints out the stats for your pokemon
	void statPg()
	{
		cout << "#################################################################\n";
		cout << name << ": " << type1 << " " << type2 << " Type\n"
			<< "\t" << HP << " HP\n"
			<< "\t" << Atk << " ATK\n"
			<< "\t" << Def << " DEF\n"
			<< "\t" << Speed << " Speed\n\n";
		//loops through the vector and prints out each attack information
		for (auto iter = attackList.begin(); iter != attackList.end(); iter++)
		{
			cout << iter->getName() << " | " << iter->getPP() << " PP\n";
		}
		cout << "#################################################################\n";
	}

private:
	string name;
	string type1;
	string type2;
	int HP;
	int Atk;
	int SpAtk;
	int Def;
	int SpDEF;
	int Speed;
	vector<Attack> attackList; //vector of attacks

};

int main()
{
	//Attacks! (no need to inherit and create a new struct)
	Attack HydroPump("Hydro Pump", 120, 0, 5, 75, "Water");
	Attack TBolt("Thunderbolt", 100, 0, 10, 100, "Electric");
	Attack ElectroBall("Electro Ball", 150, 50, 10, 100, "Elecric");
	Attack FireBlast("Fire Blast", 150, 0, 5, 80, "Fire");
	Attack Crunch("Crunch", 90, 0, 30, 100, "Dark");
	Attack Superpower("Superpower", 30, 120, 10, 90, "Fighting");
	Attack Earthquake("Earthquake", 100, 0, 10, 100, "Ground");
	Attack Psychic("Psychic", 90, 0, 25, 100, "Psychic");
	Attack ShadowBall("Shadow Ball", 90, 0, 30, 100, "Ghost");
	Attack RockSlide("Rock Slide", 100, 0, 10, 90, "Rock");
	Attack Explosion("Explosion", 300, 999999, 5, 100, "Normal");
	Attack StoneEdge("Stone Edge", 120, 30, 10, 100, "Ground");
	Attack Recover("Recover", 0, 0, 30, 100, "Normal");
	Attack FlashCannon("Flash Cannon", 100, 0, 20, 100, "Steel");
	Attack Scald("Scald", 100, 0, 30, 100, "Water");
	Attack IceBeam("Ice Beam", 100, 0, 20, 100, "Ice");

	//Attack lists for Pokemon
	vector<Attack> GolemAttack{ Earthquake, RockSlide, Explosion, StoneEdge };
	vector<Attack> ShedinjaAttack{ HydroPump, TBolt, Psychic, FireBlast };
	vector<Attack> LuxrayAttack{ TBolt, ElectroBall, Superpower, Crunch };
	vector<Attack> StarmieAttack{ TBolt, HydroPump, Recover, Psychic };
	vector<Attack> EmpoleonAttack{ Scald, FlashCannon, HydroPump, IceBeam };

	//Pokemon!
	Pokemon Golem("Golem", "Rock", "Ground", 80, 120, 130, 55, 65, 45, GolemAttack);
	Pokemon Luxray("Luxray", "Electric", "\0", 80, 120, 79, 95, 79, 70, LuxrayAttack);
	Pokemon Starmie("Starmie", "Water", "Psychic", 60, 75, 85, 100, 85, 115, StarmieAttack);
	Pokemon Empoleon("Empoleon", "Steel", "Water", 84, 86, 88, 111, 101, 60, EmpoleonAttack);
	Pokemon Shedinja("Shedinja", "Ghost", "Bug", 1, 90, 45, 30, 30, 40, ShedinjaAttack);


	// Actual stuff below
	 
	string input;
	string input2;
	Pokemon* YourPKMN = nullptr;
	Pokemon* EnemyPKMN = nullptr;
	
	cout << "Welcome to the Pokemon Battle Simulator! Player 1, choose your Pokemon! (Has to be spelled correctly, capital first letter)\n";
	cin >> input;
	cout << endl;

	if (input == "Golem") {
		YourPKMN = &Golem;
	}
	else if (input == "Luxray"){
		YourPKMN = &Luxray;
	}
	else if (input == "Starmie") {
		YourPKMN = &Starmie;
	}
	else if (input == "Empoleon") {
		YourPKMN = &Empoleon;
	}
	else if (input == "Shedinja") {
		YourPKMN == &Shedinja;
	}

	cout << "Player 2, choose your Pokemon!\n";
	cin >> input2;
	if (input2 == "Golem") {
		EnemyPKMN = &Golem;
	}
	else if (input2 == "Luxray") {
		EnemyPKMN = &Luxray;
	}
	else if (input2 == "Starmie") {
		EnemyPKMN = &Starmie;
	}	 
	else if (input2 == "Empoleon") {
		EnemyPKMN = &Empoleon;
	}
	else if (input2 == "Shedinja") {
		EnemyPKMN == &Shedinja;
	}
	
	do 
	{
		"Player 1, choose a move!\n";
		for (auto Move = Pokemon.attackList.begin(); Move != Pokemon.attackList.end(); Move++)
		{
			cout << Move->getName() << " | " << Move->getPP() << " PP\n";
		}
	} while (YourPKMN && EnemyPKMN != nullptr);
	
}
I am trying to call a vector from the class "Attack", which is inherited by the class "Pokemon", but I am having trouble. How would I properly call this vector?


1) No, Attack isn't inherited by Pokemon. And nor should it be. You probably ought to look up what inheritance actually means in OOP, to avoid confusing yourself and others.

2) You don't "call" a vector. It's not a function. I know this sounds pedantic, but if you misuse technical terms, you'll confuse yourself and others.

3) You access the vector just like you access any other member of an object. There's nothing magical about vectors that makes them not obey the normal rules of C++.

Since attackList is a private member, you can't just access it directly. You need to provide accessor methods, just like you've done for other private members of your classes.

Last edited on
YourPKMN->attackList
@MikeyBoy, I'm fairly new at c++, forgive me for my misuse of terms. Thank you both!
No problem. I'm really not trying to nitpick; these are important concepts, and it helps to be able to think clearly and precisely about them.

Repeater wrote:
YourPKMN->attackList

That would work if attackList were public, but it isn't.
Last edited on
@Ninjacop

Just some other ideas to improve your code a bit:

Consider splitting your code to use cpp and hpp files.

In your constructor, I would do this:

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
class Pokemon
{
public:
	//constructor with all the needed information to create a pokemon
	Pokemon(   // const parameters, by ref for classes or containers
           const std::string& pokeNameArg, // I put Arg so then the parameter name and member name are similar
           const std::string& type1Arg, 
           const std::string& type2Arg, 
           const int HPstatArg, // don't abbreviate identifiers, I shouldn't have to guess what they mean - they should be self documenting
           const int ATKstatArg, 
           const int SpATKstatArg, 
           const int DEFstatArg, 
           const int SpDEFstatArg, 
           const int SpeedstatArg, 
           std::vector<Attack>& attacks
           ) :
		name(pokeNameArg),
		type1(type1),
		type2(type2),
		HP(HPstat),
		Atk(ATKstat),
		SpAtk(SpATKstat),
		Def(DEFstat),
		SpDEF(SpDEFstat),
		Speed(Speedstat),
		attackList(attacks)
	{}


I put each parameter on it's own line, I find it easier to read that way.

If there are lots of parameters, that's a hint that it could done a better way: Consider putting all the stats into a struct, then pass that as one of the arguments.

Try to get away from using namespace std; It will bite you one day; it defeats the purpose of namespaces. Just put std:: before each std thing ; that's what all the experienced people do. Their is bulk info about this on the web: Google it.

Good Luck !!
Topic archived. No new replies allowed.