I need a static hash table to keep track of all objects of a particular type that are instantiated in a Qt application but I have never used a template class as a static member object before and I can't seem to figure out how to initialize it. QHash is the hash table class that follows the template convetion:
template<class key, class data>
QString is probably self explanatory.
Example Header:
1 2 3 4 5 6
class MyClass
{
...
private:
static QHash<QString, MyClass*> instanceTable;
}
Here is my source that doesn't compile.
Example Source
1 2 3 4 5
#include header.h
// using default constructor for table...
QHash<QString key, MyClass* instance> MyClass::instanceTable(); // gives Error below.
// Error in above line is "Declaration is incompatible with QHash<QString, Myclass*>"
I have tried doing it a number of different ways and none of them work. How do you initialize a static template object?
Ok... I tried it making instanceTable a pointer and it works. However... I would prefer to make it a simple class instance as shown in the original example. Otherwise I will probably get a memory leak. Is this possible to do?
The template arguments should be specified the same way as in the header file, without (variable?) names. You also need to get rid of the parentheses at the end otherwise the compiler will think you are trying to define a member function.