Vector Accessor Method

Hello! I was having trouble making an accessor method for a vector of the class below. How would I do it?

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
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

};
Are you looking for a accessor to return the attackList or a accessor to a vector of type vector<Pokémon>?
@knowclue! It's you again! I'm looking for an accessor to return the attackList, so when I want to make a battle loop (Which is shown below), I can show the attacks used by the "Pokemon" object. (At the do/while loop)

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
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);
	
}
Last edited on
1) Please stop creating new threads for the same problem. You already have:

http://www.cplusplus.com/forum/general/223789/

where we're discussing this issue.

2) Can you be more specific about what the problem is? You clearly know how to write accessor methods for private data members, because you've already written some for your Attack class that you posted in that other thread. What problems are you having doing the same for this one member?
@MikeyBoy, I do not know how to write an accessor method for a vector.
@Ninjacop I agree with MikeyBoy you do not need to keep creating new threads for further questions. Just add them to the same one. People will see the addition and help you.

As Mikeyboy said, you just need to do the same thing that you have for the accessors in your attack class. then you could access it through main in the following code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
	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");

	vector<Attack> golemAttack{HydroPump,TBolt,ElectroBall,FireBlast};
	Pokemon golem("Golem","Rock","Ground",100,90,80,70,60,50,golemAttack);
	
	vector<Attack> test = golem.getAttackList();
	
	for (auto iter = test.begin(); iter != test.end(); iter++)
	{
		cout << iter->getName() << " | " << iter->getPP() << " PP\n";
	}
	return 0;
}
Alright. Thank you both, this issue has finally been solved. No new threads needed. I just thought that this was a little different than the thread before, so I created a new one. No harm done, pretty new to the site.
@Ninjacop Think about it for a second. To return name in your attack class the function returns the member variable name which is a string type. For the accessor to the attackList, you need to return attack liust, and your return type needs to be the same type as attackList.
Topic archived. No new replies allowed.