vector of classes and how to access them properly

So far I got how to create the classes ok today but I would like a vector of classes(I think). im trying to access the virtual functions with a single command to different monsters of class critters. some help on the subject would be great. I cant find what im looking for online.



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
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
#include "Critter.h"
#include "goblin.h"

using namespace std;

int main()
{
	Critter* mob = new Critter[3];
	
	for(int i = 0; i < 3; ++i)
	{mob[i].~Critter();}
	mob[0]=goblin();
	mob[1]=Critter();
		cout << mob[0].gethp() << " hp's" << endl;
	cout << mob[0].getname() << " name\n";
	cout << mob[1].gethp() << " hp's" << endl;
	cout << mob[1].getname() << " name\n";
	
	cout << "Lets call attack for critter 0:\n";
	mob[0].MobAtt();
	cout << "Lets call attack for critter 1:\n";
	mob[1].MobAtt();
	
	
	
	system("pause");
	return 0;
} 



critter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include<string>

class Critter
{
protected:
	std::string name;
	int HP;

public:
	Critter(void);
	~Critter(void);
	void setname(std::string n);
	std::string getname();
	void sethp(int h);
	int gethp();
	virtual void MobAtt();

};


critter.cpp
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
#include "Critter.h"
#include <iostream>
#include <string>

Critter::Critter(): name("rat"), HP(10)
{
}


Critter::~Critter(void)
{
}
void Critter::setname(std::string n)
{
	name = n;
}
std::string Critter::getname()
{
	return name;
}
int Critter::gethp()
{
	return HP;
}
void Critter::sethp(int h)
{
	HP=h;
}
void Critter::MobAtt()
{
	std::cout << "Monster attacks u for 3 points" << std::endl;
}


goblin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#pragma once
#include "critter.h"
class goblin :
	public Critter
{
public:
	goblin(void);
	~goblin(void);
	virtual void MobAtt();
};




goblin.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include "goblin.h"
#include <iostream>

goblin::goblin(void)
{
	goblin::setname("goblin"), goblin::sethp(4);
}


goblin::~goblin(void)
{
}

void goblin::MobAtt()
{
	std::cout << "goblin takes his sword and over hand chops you in the head for 20 points" << std::endl;
}
First off, in line 15 of your main.cpp, why are you calling the destructors? I'm not even sure if that's legal...

Anyway, for a vector of classes, you obviously need a vector. Considering you are storing polymorphic classes, you need to store pointers to them. Here is how I would do your 'main.cpp':
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
#include <iostream>
#include <vector>
#include <memory>
#include "Critter.h"
#include "goblin.h"

int main() {
    // list of Critter smart pointers
    std::vector<std::unique_ptr<Critter>> mobs;
    mobs.emplace_back(new goblin);
    mobs.emplace_back(new Critter);

    for (auto& x : mobs) { 
        std::cout << x->gethp() << " hp\n";
        std::cout << x->getName() << " name\n";
    }
	
    std::cout << "Lets call attack for critter 0:\n";
    mobs[0]->MobAtt();
    std::cout << "Lets call attack for critter 1:\n";
    mobs[1]->MobAtt();
	
    std::cin.sync();
    std::cin.get();
    return 0;
} 


Also, you need to do some looking up into how classes work, namely your goblin constructor. Initializer lists could be something to learn, too. For now, get rid of the goblin:: in setname and sethp for your goblin class constructor.
thank u very much, that worked for storing the monsters and calling there attacks.

the for (auto& x : mobs) gave me a error: can not deduce 'auto' type (initializer required), but I looped through with:

unsigned int size = mobs.size();
for (unsigned int i = 0; i < size; ++i)
{
cout << mobs[i]->getname() << endl;
}

and ill check out the initializer list. I just read some stuff on classes and so I wanted to try it out and well I had got that far and the book ended, it didn't explain how to store a polymorphic class in a vector. u have a name of a good book this stuff?

Topic archived. No new replies allowed.