Hello,
I have the following code with a template class and a STL map but it gives me an error.
The code :
[code]#include
using namespace std;
template <typename Key, class Object>
class TObjectFactory
{
public:
typedef Object *(*Creator)();
map<Key, Creator> fRegisteredCreator;
bool Register(Key key, Creator creator)
{
if( fRegisteredCreator.find(key) != fRegisteredCreator.end() )
return false;
fRegisteredCreator.insert(pair<Key, Creator>(key, creator));
return true;
}
Object* Create(Key key)
{
Object *object;
Creator creator;
map<Key, Creator>::iterator it;
it = fRegisteredCreator.find(key);
if(it == fRegisteredCreator.end())
return NULL;
creator = (*it).second;
object = (*creator)();
return object;
}
};[/code]
The error:
error: expected `;’ before ‘it’
He seems to ignore my iterator declaration. Is root incompatible with the STL&Templates?
anar
2
[quote=“f56bre”]Hello,
I have the following code with a template class and a STL map but it gives me an error.
The code :
[code]#include
using namespace std;
template <typename Key, class Object>
class TObjectFactory
{
public:
typedef Object *(*Creator)();
map<Key, Creator> fRegisteredCreator;
bool Register(Key key, Creator creator)
{
if( fRegisteredCreator.find(key) != fRegisteredCreator.end() )
return false;
fRegisteredCreator.insert(pair<Key, Creator>(key, creator));
return true;
}
Object* Create(Key key)
{
Object *object;
Creator creator;
map<Key, Creator>::iterator it;
it = fRegisteredCreator.find(key);
if(it == fRegisteredCreator.end())
return NULL;
creator = (*it).second;
object = (*creator)();
return object;
}
};[/code]
The error:
error: expected `;’ before ‘it’
He seems to ignore my iterator declaration. Is root incompatible with the STL&Templates?[/quote]
One of the problem I see in your code is that you use
without typename key word.
iterator may be interpreted as a static member of the class map<Key, Creator>. To make it clear you need to write
It has nothing to do with ROOT, it is just C++.
P.S. You may want to define something like typedef map<Key, Creator> container_t; Otherwise you will be forced always write definition of your map.
Perfect, it compiles now,
thank you very much anar.
anar
4
[quote=“f56bre”]Perfect, it compiles now,
thank you very much anar.[/quote]
