Problem declaring member functions with class template
Nov 27, 2013 at 9:08am UTC
Whats wrong with my member function Greet()? I am using a class template so i can have instances of my class Critter in a vector array. My problem is that i can't figure out how to declare member functions when using a class template. Any suggestions would be appreciated? Thanks!
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 <vector>
using namespace std;
template <typename T>
class Critter : public vector<T>
{
public :
int m_Hunger;
void Greet(void );
};
void Critter::Greet()
{
cout << "Hi. I'm a critter. My hunger level is " << m_Hunger << ".\n" ;
}
int main()
{
Critter<int > crit_array;
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.push_back( crit_array.size() ); //5
system("pause" );
return 0;
}
This code gives me the following error:
Error 1 error C2955: 'Critter' : use of class template requires template argument list
btw: the class is derived from Micheal Dawsons book Beginning C++ Through Game Programming
Last edited on Nov 27, 2013 at 9:08am UTC
Nov 27, 2013 at 9:16am UTC
Your function should be:
1 2 3 4
template <typename T> void Critter<T>::Greet()
{
cout << "Hi. I'm a critter. My hunger level is " << m_Hunger << ".\n" ;
}
Hope this helps. :-)
Last edited on Nov 27, 2013 at 9:16am UTC
Nov 27, 2013 at 9:23am UTC
Works perfect thank you very much, I greatly appreciate it!
Topic archived. No new replies allowed.