C++ template problem [Solved]
Hello, i am in the process of making a small game as a personnal project and am currently building a system to manage game ressources such as textures, models, sounds...
So i figured i would use C++ templates to do so.
I made a template <typename T> Ressource<T> class which holds a pointer to the said ressource (sound, texture ... ) aswell as a reference count and some really basic functionnality.
Above that i have a RessourceManager<T> class that manages ressources of type T using a std::map and reference counting mecanisms.
Now what i would like to do is the following (inside the RessourceManager<T> class):
std::map<std::string, Ressource<T>*> ressource_map;
A ressource would be added by name in the ressource manager, if the name already exists dont add the ressource, just use it.
The problem is that whenever i try to use an iterator on the above ressource_map, std::map<std::string, Ressource<T>*>::iterator i get the following problem:
"ressourcemanager.h:40: erreur: expected `;' before «iter""
line 40 being: std::map<std::string, Ressource<T>*>::iterator iter;
So can i use 'nested' templates like this? (template inside an std::map).
Is there a simpler way to make a Ressource manager?
Thanks in advance.
[Edited by - Saya on February 28, 2008 10:29:24 AM]
The problem is that the compiler cannot figure out whether std::map<std::string,Resource<T> *>::iterator is a type or a member.
The solution is to tell the compiler using the typename keyword:
The solution is to tell the compiler using the typename keyword:
template<typename T>class Foo{ typedef std::map<std::string, Ressource<T>*> ResourceMap; ResourceMap resourceMap;public: void bar(const std::string &name) { // Hey compiler! // "it" is a type, deal with it typename ResourceMap::iterator it = resourceMap.find(name); if( it != resourceMap.end() ) { // use *it } }};
The iterator is dependant on the template type.
edit: too slow
Anyway I would also typedef the iterator
typename std::map<std::string, Ressource<T>*>::iterator iter;
edit: too slow
Anyway I would also typedef the iterator
template<typename T>class Foo{ typedef std::map<std::string, Ressource<T>*> ResourceMap; typedef typename ResourceMap::iterator iter; ResourceMap resourceMap;public: void bar(const std::string &name) { iter it = resourceMap.find(name);...
Thanks alot guys, you answered fast! It compiles fine now :) !
Btw you made me realise that Resource takes only one s in english! thanks for that too (french here...).
Btw you made me realise that Resource takes only one s in english! thanks for that too (french here...).
In the code snippet there you have misspelt 'Resource', but other than that I see no problems. To make life and maintenance easier on yourself, you should really typedef these nested templates though:
(Edit: Gah, too slow [smile])
#include <map>template <typename T>class ResourceManager{ typedef Resource<T> *value_t; typedef std::map<std::string, value_t> map_t; typedef map_t::iterator iterator_t; map_t map; void do_something() { for (iterator_t it = map.begin(); it != map.end(); ++it) ; }};
(Edit: Gah, too slow [smile])
Thanks switfcoder i'll do it that way.
Btw it should be :
typedef typename map_t::iterator iterator_t;
no?
Quote:Original post by Saya
Btw it should be :
typedef typename map_t::iterator iterator_t;
no?
In theory, maybe, but in practice I have never had GCC fail to detect this.
[irrelevant]
Haha, and you have misspelled "misspelled." [smile]
[/irrelevant]
Quote:original post by swiftcoder
In the code snippet there you have misspelt 'Resource', but other than that I see no problems.
Haha, and you have misspelled "misspelled." [smile]
[/irrelevant]
Quote:Original post by Shakedown
[irrelevant]Quote:original post by swiftcoder
In the code snippet there you have misspelt 'Resource', but other than that I see no problems.
Haha, and you have misspelled "misspelled." [smile]
[/irrelevant]
Quote:misspell |mɪsˈspɛl|
verb ( past and past part. -spelled or -spelt ) [ trans. ]
spell (a word) incorrectly.
Quote:grammar |ˈgramə|
noun
A subject formerly taught at the pre-graduate level.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement