I am currently working on template classes (and I am quite lost). I want to build a template class that can stores objects.
Here is my .h file :
#ifndef STORAGE_H_
#define STORAGE_H_
#include <iostream>
using namespace std;
template<class T,int maxsize>
class Storage
{
public:
Storage();
int getSize();
T get(int index);
bool add(T element);
private:
T array[maxsize];
int length;
};
template<class T,int maxsize>
Storage<T,maxsize>::Storage()
: length(0),// Liste d'initialisation des données membres
{}
template<class T,int maxsize>
int Storage<T,maxsize>::getSize()
{
return length;
}
template<class T,int maxsize>
T Storage<T,maxsize>::get(int index)
{
return array[index];
}
template<class T,int maxsize>
bool Storage<T,maxsize>::add(T element)
{
if (length>=maxsize) return false;
array[length++]=element;
return true;
}
#endif /* STORAGE_H_ */
It works fine when i'm dealing with int for instance. But when it comes to others objects I previously made it fails. In my main .cpp file :
Storage<Pokemon,4> pokemonArray;
Which sends me the following error :
no matching function for call to 'Pokemon::Pokemon()'
Pokemon being a class I have. To me this line means that it can't find the constructor Pokemon().
How can I make it work ?
Thank you guys !