Sign in to follow this  
Saya

C++ template problem [Solved]

Recommended Posts

Saya    112
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]

Share this post


Link to post
Share on other sites
rip-off    10976
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:


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
}
}
};


Share this post


Link to post
Share on other sites
CmpDev    100
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);
...

Share this post


Link to post
Share on other sites
Saya    112
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...).

Share this post


Link to post
Share on other sites
swiftcoder    18432
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])

Share this post


Link to post
Share on other sites
Shakedown    230
[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]

Share this post


Link to post
Share on other sites
swiftcoder    18432
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this