Jump to content
  • Advertisement
Sign in to follow this  
Saya

C++ template problem [Solved]

This topic is 3792 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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

Thanks switfcoder i'll do it that way.
Btw it should be :
typedef typename map_t::iterator iterator_t;
no?

Share this post


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

Share this post


Link to post
Share on other sites
[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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!