Skip to main content
gamedev.net
🔒 Locked

C++ template problem [Solved]

Started by Saya Feb 28, 2008 at 9:04 AM 7 replies 1.3k views
Original Post
Saya
Saya
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 Ressource 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 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 class): std::map*> 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*>::iterator i get the following problem: "ressourcemanager.h:40: erreur: expected `;' before «iter"" line 40 being: std::map*>::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]
rip-off
rip-off
The problem is that the compiler cannot figure out whether std::map *>::iterator is a type or a member.

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         }    }};
CmpDev
CmpDev
The iterator is dependant on the template type.
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);...
Saya
Saya
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...).
swiftcoder
swiftcoder
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:
#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])
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
Saya
Saya

Thanks switfcoder i'll do it that way.
Btw it should be :
typedef typename map_t::iterator iterator_t;
no?
swiftcoder
swiftcoder
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.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
swiftcoder
swiftcoder
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.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.