Calling member functions of a class instance stored in a vector array

I am trying to call Greet() which is in one of the class Critter instances in the vector array crit_array. When trying to call it using crit_array[1].Greet(); i just get an error. Do i even have the proper syntax for the vector array to be able to handle class objects (at line 25)? Any help would be greatly appreciated. Sorry if i sound like a dumbo as i am fairly new to classes. So pretty much my question is how can i call public member functions of class instances that are stored in a vector.

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
//Vector Class Array

#include <iostream>
#include <string>
#include <vector>

using namespace std;

template <typename T>
class Critter : public vector<T>
{
    public:
		int m_Hunger;
		int m_Age;
		void Greet();
};

template <typename T> void Critter<T>::Greet()
{
   cout << "Hi. I'm a critter. I am " << m_Age << " years old. My hunger level is " << m_Hunger << ".\n";
}

int main()
{
	Critter<int> crit_array;

	crit_array.push_back( crit_array.size() ); //0
	crit_array.push_back( crit_array.size() ); //1
	crit_array.push_back( crit_array.size() ); //2
	crit_array.push_back( crit_array.size() ); //3
	crit_array.push_back( crit_array.size() ); //4
	
	crit_array[1].Greet();

	system("pause");

    return 0;
}


This code returns the following error(s):

1.
Error 1 error C2228: left of '.Greet' must have class/struct/union (at line 33)
2.
2 IntelliSense: expression must have class type (at line 33)

btw: The class was derived from Michael Dawson's book Beginning C++ Through Game Programming.
Last edited on
Critter is a vector, so crit_array is a vector templated on int.

So crit_array[1] is an int, not an object of a class type.

EDIT: It is crit_array itself that has the Greet() method, not the elements of its vector.
Last edited on
Topic archived. No new replies allowed.