Declaring a global variable of a template type
-
Hi there I am writing a program to use linked lists using templates, and i am trying to make it as fast as possible, so i am thinking of storing the address of the last inserted item. Each node of the list if declared as follows: template struct NodeList { ItemType *item; NodeList *next; } what i am trying to do is to declare a global variable like template NodeList *last_inserted=NULL; , but the compiler won't allow me to do that. If instead i declare it as NodeList<> *last_inserted=NULL, it is ok, but it would work for integers only. How can I get this to work??? Thanks
-
Hi there I am writing a program to use linked lists using templates, and i am trying to make it as fast as possible, so i am thinking of storing the address of the last inserted item. Each node of the list if declared as follows: template struct NodeList { ItemType *item; NodeList *next; } what i am trying to do is to declare a global variable like template NodeList *last_inserted=NULL; , but the compiler won't allow me to do that. If instead i declare it as NodeList<> *last_inserted=NULL, it is ok, but it would work for integers only. How can I get this to work??? Thanks
Have you thought about using the STL to do this? It does exactly what you need, is about as fast as you can get, and most importantly, it works without you needing to re-invent something. An example can be found here[^]. If you really must create your own linked list, then there is no need to use a global (which is a bad thing to do anyway) for the next position. Instead, store it in the class that provides the list operations.
-
Have you thought about using the STL to do this? It does exactly what you need, is about as fast as you can get, and most importantly, it works without you needing to re-invent something. An example can be found here[^]. If you really must create your own linked list, then there is no need to use a global (which is a bad thing to do anyway) for the next position. Instead, store it in the class that provides the list operations.