传递比较仿函数时C ++ std :: map编译器错误

时间:2012-08-20 14:04:59

标签: c++ compare functor stdmap

我在某个地方做了一个不好的假设我相信。我假设std::map只会在索引中进行比较。但是,不是试图比较两个uint64,而是试图将其与其他东西进行比较(参见编译器错误)。

  struct Tag { };
  typedef Handle<Tag> HPowerType;
  class PowerTypeMgr
  {
    struct PowerType { /** stuff **/ };
    struct uint64_less 
    {
        bool operator() ( const uint64& l, const uint64& r) const
        {
            return ((l<r)?true:false);
        }
    };

   typedef std::map <uint64,HPowerType,uint64_less> PTHMap;
  };

导致此编译器错误:

    c:\cry_v340_3696\code\sdks\stlport\stlport\stl\debug\_tree.h(62): error C2664:
    'bool PowerTypeMgr::uint64_less::operator ()(const uint64 &,const uint64 &) const' : 
    cannot convert parameter 2 from 'PowerTypeMgr::PowerType *const ' to 'const uint64&'

   2>          Reason: cannot convert from 'PowerTypeMgr::PowerType *const ' to 'const 
   uint64'
   2>          There is no context in which this conversion is possible
   2>          c:\cry_v340_3696\code\sdks\stlport\stlport\stl\_tree.h(570) : see         
   reference to function template instantiation 'bool    
   stlpd_std::priv::_DbgCompare<_Key,_Compare>::operator ()<_Key,_KT>(const _Kp1 &,const 
   _Kp2 &) const' being compiled

   2>          with
   2>          [
   2>              _Key=unsigned __int64,
   2>              _Compare=PowerTypeMgr::uint64_less,
   2>              _KT=PowerTypeMgr::PowerType *,
   2>              _Kp1=unsigned __int64,
   2>              _Kp2=PowerTypeMgr::PowerType *
   2>          ]

这是完整的用法,因为我似乎还没有发布足够的内容。注意,如果我没有传递std :: map的仿函数,我会得到同样的错误。我会在这里发布另一种方式。

class PowerTypeMgr
{
public:

    virtual ~PowerTypeMgr();

    PowerTypeMgr& Instance()
    {
        static PowerTypeMgr Obj;
        return Obj;
    }

    HPowerType StorePowerType ( 
        uint64 EMP, uint64 ESP, int wC, int chiC, int hC, const char* MPName, 
        const char* SPName, bool isC, bool unlock,const char* PN,const char* PC,
        const char* FMT
    );

    void DeletePowerType(HPowerType Hpt);   //not needed for powertypes, 
                                            //they should always be loaded or unloaded together

    uint64 makePowerString(EMainPowers EMP, ESubPowers ESP);

    bool    IsComboable(HPowerType Hpt) const;
    string  GetPowerName(HPowerType Hpt) const;
    string  GetMPEffectName(HPowerType Hpt) const;
    string  GetSPEffectName(HPowerType Hpt) const;
    string  GetProjectileClass(HPowerType Hpt) const;
    string  GetFireModeType(HPowerType Hpt) const;
    int     GetHealthCost(HPowerType Hpt) const;
    int     GetCHICost(HPowerType Hpt) const;
    int     GetWeaponCost(HPowerType Hpt) const;
    bool    IsUnlocked(HPowerType Hpt) const;
    uint64  GetPowerString(HPowerType Hpt) const;

    // Only things that may change during the course of game execution //
    void    StorePowerName(HPowerType Hpt, string pn);
    void    Unlock(HPowerType Hpt);//, bool L);             //true = unlock it
    void    Lock(HPowerType Hpt);

    bool SaveGame(/* GamePak, SaveName*/) {}//TODO
    bool LoadGame(/* GamePak, SaveName*/) {}//TODO


        private:

    struct PowerType 
    {
        uint64 thePower; //64-bit string where uppermost 8 bits is the EMainPower, and lower 56 is ESubPower
        EMainPowers mainPower; //currently unused
        ESubPowers subPower; //ditto

        bool isComboable;
        bool isUnlocked;
        int weaponCost; //weapon cost (to hold)
        int CHIcost; //cost to CHI meter to use it.
        int healthCost; //additional healthCost to use it
        string powerName;
        string MPEffectName;
        string SPEffectName;
        string projectileClass;
        string fireModeType;

        //MEMBERS --------------------------------------------------<<<
        void Unload();

        EMainPowers getMainPower() const;
        EMainPowers convertToMainPower(const uint64& bitstring);
        ESubPowers getSubPower() const;
        ESubPowers convertToSubPower(const uint64& bitstring);

    }; //end struct PowerType
    //--------------------------------------------------------------------------------------------------------


    PowerTypeMgr(); //SINGLETON pattern


    typedef std::pair<uint64,HPowerType>        PowerMap_Pair;  
    typedef std::map <uint64,HPowerType>        PTHMap;//,int64_less>       PTHMap; //,uint64_less>     PTHMap;         //O(logn) look up bitstr
    PTHMap                                      m_PTHMap;       //...

    typedef std::pair<PTHMap::iterator,bool>    indexInsertRC;

    typedef HandleMgr <PowerType, HPowerType>   PowerHandle;
    PowerHandle                                 m_PowerTypeHMgr;

    bool                                        inited;             //Used in assert & checking if trying to load more than once
    int                                         totalPowerTypes;
        }; //end PowerTypeMgr

这是编译器错误(VC Express 2010)

   '2>  PowerTypeMgr.cpp
    2>c:\cry_v340_3696\code\sdks\stlport\stlport\stl\debug\_tree.h(62): error C2664: 'bool stlpd_std::less<_Tp>::operator ()(const _Tp &,const _Tp &) const' : cannot convert parameter 2 from 'PowerTypeMgr::PowerType *const ' to 'const uint64 &'
    2>          with
    2>          [
    2>              _Tp=uint64
    2>          ]
    2>          Reason: cannot convert from 'PowerTypeMgr::PowerType *const ' to 'const uint64'
    2>          There is no context in which this conversion is possible
    2>          c:\cry_v340_3696\code\sdks\stlport\stlport\stl\_tree.h(570) : see reference to function template instantiation 'bool stlpd_std::priv::_DbgCompare<_Key,_Compare>::operator ()<_Key,_KT>(const _Kp1 &,const _Kp2 &) const' being compiled
    2>          with
    2>          [
    2>              _Key=unsigned __int64,
    2>              _Compare=stlpd_std::less<uint64>,
    2>              _KT=PowerTypeMgr::PowerType *,
    2>              _Kp1=unsigned __int64,
    2>              _Kp2=PowerTypeMgr::PowerType *
    2>          ]
    2>          c:\cry_v340_3696\code\sdks\stlport\stlport\stl\_tree.h(560) : see reference to function template instantiation 'stlpd_std::priv::_NonDbg_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>::_Base_ptr stlpd_std::priv::_NonDbg_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>::_M_find<_KT>(const _KT &) const' being compiled
    2>          with
    2>          [
    2>              _Key=unsigned __int64,
    2>              _Compare=stlpd_std::priv::_DbgCompare<unsigned __int64,stlpd_std::less<uint64>>,
    2>              _Value=stlpd_std::pair<const uint64,HPowerType>,
    2>              _KeyOfValue=stlpd_std::priv::_Select1st<stlpd_std::pair<const uint64,HPowerType>>,
    2>              _Traits=stlpd_std::priv::_MapTraitsT<stlpd_std::pair<const uint64,HPowerType>>,
    2>              _Alloc=stlpd_std::allocator<stlpd_std::pair<const uint64,HPowerType>>,
    2>              _KT=PowerTypeMgr::PowerType *
    2>          ]
    2>          c:\cry_v340_3696\code\sdks\stlport\stlport\stl\debug\_tree.h(178) : see  
    reference to function template instantiation 'stlpd_std::priv::_Rb_tree_iterator<_Value,_Traits> stlpd_std::priv::_NonDbg_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>::find<_KT>(const _KT &)' being compiled
    2>          with
    2>          [
    2>              _Value=stlpd_std::pair<const uint64,HPowerType>,
    2>              _Traits=stlpd_std::priv::_MapTraitsT<stlpd_std::pair<const uint64,HPowerType>>,
    2>              _Key=unsigned __int64,
    2>              _Compare=stlpd_std::priv::_DbgCompare<unsigned __int64,stlpd_std::less<uint64>>,
   2>              _KeyOfValue=stlpd_std::priv::_Select1st<stlpd_std::pair<const uint64,HPowerType>>,
    2>              _Alloc=stlpd_std::allocator<stlpd_std::pair<const uint64,HPowerType>>,
   2>              _KT=PowerTypeMgr::PowerType *
   2>          ]
   2>          c:\cry_v340_3696\code\sdks\stlport\stlport\stl\_map.h(214) : see reference to function template instantiation 'stlpd_std::priv::_DBG_iter<_Container,_Traits> stlpd_std::priv::_Rb_tree<_Key,_Compare,_Value,_KeyOfValue,stlpd_std::priv::_MapTraitsT<_Tp>,_Alloc>::find<_KT>(const _KT &)' being compiled
   2>          with
   2>          [
   2>              _Container=stlpd_std::priv::_NonDbg_Rb_tree<unsigned __int64,stlpd_std::priv::_DbgCompare<unsigned __int64,stlpd_std::less<uint64>>,stlpd_std::pair<const uint64,HPowerType>,stlpd_std::priv::_Select1st<stlpd_std::pair<const uint64,HPowerType>>,stlpd_std::priv::_MapTraitsT<stlpd_std::pair<const uint64,HPowerType>>,stlpd_std::allocator<stlpd_std::pair<const uint64,HPowerType>>>,
   2>              _Traits=stlpd_std::priv::_DbgTraits<stlpd_std::priv::_MapTraitsT<stlpd_std::pair<const uint64,HPowerType>>>,
    2>              _Key=unsigned __int64,
    2>              _Compare=stlpd_std::less<uint64>, 
    2>              _Value=stlpd_std::pair<const uint64,HPowerType>,
    2>              _KeyOfValue=stlpd_std::priv::_Select1st<stlpd_std::pair<const uint64,HPowerType>>,
    2>              _Tp=stlpd_std::pair<const uint64,HPowerType>,
    2>              _Alloc=stlpd_std::allocator<stlpd_std::pair<const uint64,HPowerType>>,
    2>              _KT=PowerTypeMgr::PowerType *
    2>          ]
    2>          c:\cry_v340_3696\code\game\gamedll\monk\powertypemgr.cpp(78) : see reference to function template instantiation 'stlpd_std::priv::_DBG_iter<_Container,_Traits> stlpd_std::map<_Key,_Tp>::find<PowerTypeMgr::PowerType*>(const _KT &)' being compiled
    2>          with
     2>          [
    2>              _Container=stlpd_std::priv::_NonDbg_Rb_tree<unsigned __int64,stlpd_std::priv::_DbgCompare<unsigned __int64,stlpd_std::less<uint64>>,stlpd_std::pair<const uint64,HPowerType>,stlpd_std::priv::_Select1st<stlpd_std::pair<const uint64,HPowerType>>,stlpd_std::priv::_MapTraitsT<stlpd_std::pair<const uint64,HPowerType>>,stlpd_std::allocator<stlpd_std::pair<const uint64,HPowerType>>>,
    2>              _Traits=stlpd_std::priv::_DbgTraits<stlpd_std::priv::_MapTraitsT<stlpd_std::pair<const uint64,HPowerType>>>,
    2>              _Key=uint64,
    2>              _Tp=HPowerType,
    2>              _KT=PowerTypeMgr::PowerType *
    2>          ]
    2>c:\cry_v340_3696\code\sdks\stlport\stlport\stl\debug\_tree.h(62): error C2664: 'bool stlpd_std::less<_Tp>::operator ()(const _Tp &,const _Tp &) const' : cannot convert parameter 1 from 'PowerTypeMgr::PowerType *const ' to 'const uint64 &'
    2>          with
    2>          [
 2>              _Tp=uint64
 2>          ]
 2>          Reason: cannot convert from 'PowerTypeMgr::PowerType *const ' to 'const uint64'
 2>          There is no context in which this conversion is possible
 2>          c:\cry_v340_3696\code\sdks\stlport\stlport\stl\_tree.h(576) : see reference to function template instantiation 'bool stlpd_std::priv::_DbgCompare<_Key,_Compare>::operator ()<_KT,_Key>(const _Kp1 &,const _Kp2 &) const' being compiled
2>          with
 2>          [
 2>              _Key=unsigned __int64,
 2>              _Compare=stlpd_std::less<uint64>,
 2>              _KT=PowerTypeMgr::PowerType *,
 2>              _Kp1=PowerTypeMgr::PowerType *,
 2>              _Kp2=unsigned __int64
 2>          ]
 2>  PowerWeapon.cpp
  ...'

0 个答案:

没有答案