Petit problème de template
Bonjour,
Je démarre dans l'utilisation des templates. J'ai réalisé quelques fonctions template avec succès. J'essaye maintenant généraliser une de mes classe outil par l'utilisation d'un template, mais je rencontre quelques difficultés. Peut-être pouvez-vous me conseiller?
Cette classe est simpement une réencapsulation d'un multimap avec quelques fonctions de manipulation suplémentaire
Fichier h original:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
class CRankEx
{
public:
CRankEx();
~CRankEx();
protected:
typedef std::pair<_string, LPARAM> ItemPair;
typedef std::pair<double,ItemPair> MMapPair;
typedef std::multimap<double, ItemPair> _mmap;
_mmap::const_iterator m_It;
_mmap m_mmap;
// Functions
public:
void Add(const _string& str, double dKey, LPARAM itemdata=0, bool bAddIfExist=true);
....
};
et le .cpp
CRankEx::CRankEx()
{
m_It = m_mmap.end();
}
void CRankEx::Add(const _string& str, double dKey, LPARAM itemdata/*=0*/, bool bAddIfExist/*=true*/)
{
_mmap::iterator it;
if (!bAddIfExist)
{
it=m_mmap.find(dKey);
if (it != m_mmap.end()) // Item found
return;
};
m_mmap.insert(MMapPair(dKey,ItemPair(str,itemdata)));
}
.... |
La clef de tri est un double. Mon but est de remplacer celle-ci par un template. J'ai donc:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
template<typename T>
class CRankEx
{
public:
CRankEx();
~CRankEx();
protected:
typedef std::pair<_string, LPARAM> ItemPair;
typedef std::pair<T,ItemPair> MMapPair;
typedef std::multimap<T, ItemPair> _mmap;
_mmap::const_iterator m_It;
_mmap m_mmap;
// Functions
public:
void Add(const _string& str, T dKey, LPARAM itemdata=0, bool bAddIfExist=true);
....
};
et le .cpp
template<typename T>
CRankEx::CRankEx()
{
m_It = m_mmap.end();
}
template<typename T>
void CRankEx::Add(const _string& str, T dKey, LPARAM itemdata/*=0*/, bool bAddIfExist/*=true*/)
{
_mmap::iterator it;
if (!bAddIfExist)
{
it=m_mmap.find(dKey);
if (it != m_mmap.end()) // Item found
return;
};
m_mmap.insert(MMapPair(dKey,ItemPair(str,itemdata)));
}
.... |
Il semble que la déclaration
Code:
typedef std::multimap<T, ItemPair> _mmap;
ne soit pas correct.
Quel serait votre conseil?
Merci