Hello! guys
I want to make class which can be used to create a collection of other classes.
For example I have a class Image, now i want to make a class which can be used to store multiple images. I have made the following Manager class
template <class t>
class Manager
{
std::vector<t> items;
int count;
public:
Manager();
~Manager();
int Add(t &item);
t *GetItem(int index);
};
template <class t>
t *Manager<t>::GetItem(int index)
{
return &items[index];
}
Now when i used
Manager<Image> images;
It gave me following error
build/Release/MinGW-Windows/GFX.o:GFX.cpp:(.text+0xe): undefined reference to `Manager<Image>::~Manager()'
build/Release/MinGW-Windows/GFX.o:GFX.cpp:(.text+0x291): undefined reference to `Manager<Image>::GetItem(int)'
build/Release/MinGW-Windows/GFX.o:GFX.cpp:(.text+0xe4d): undefined reference to `Manager<Image>::Add(Image&)'
build/Release/MinGW-Windows/GFX.o:GFX.cpp:(.text+0xf66): undefined reference to `Manager<Image>::Manager()'
Are your method definitions in the same file as your class/method declarations?
Read the bottom section "Templates and multiple-file projects": http://cplusplus.com/doc/tutorial/templates
I want to make class which can be used to create a collection of other classes.
For example I have a class Image, now i want to make a class which can be used to store multiple images.
To be honest those two don't sound like the same thing. Your 'manager class' sounds like a Factory Class which creates objects of different types (classes), but your example is actually a Container. That's not the really same thing.
EDIT:
Actually I think I now understand what you are saying. It is the collection that you want to create. So ignore my comment :)
Mathhead200 is right, these are linking errors. Method definitions should be in a seperate .cpp file or the same header file as the declarations, but never in a different header file.