首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

stl set有关问题

2014-01-17 
stl set问题最近需要使用到有序集合,使用了SET。遇到编译不过的初级问题,代码如下。请帮忙看看,感激不尽,。。#

stl set问题
最近需要使用到有序集合,使用了SET。
遇到编译不过的初级问题,代码如下。请帮忙看看,感激不尽,stl set有关问题



#include <set>

class Player
{
public:
Player(const Player& player)
{
this->m_experience = player.m_experience;
this->m_gpID = player.m_gpID;
this->m_level = player.m_level;
this->m_ulPlayerID = player.m_ulPlayerID;
};
Player(const unsigned long playerID, const unsigned long gpID, 
const unsigned long experience, const unsigned char level):
m_ulPlayerID(playerID),m_gpID(m_gpID), 
m_experience(experience), m_level(level){}
bool operator < (const Player &player) const
{
return (this->m_level<player.m_level);
}
Player& operator = (const Player &player)


{
if (&player == this)
{
return (*this);
}
this->m_experience = player.m_experience;
this->m_gpID = player.m_gpID;
this->m_level = player.m_level;
this->m_ulPlayerID = player.m_ulPlayerID;
return (*this);
}
private:
unsigned long m_ulPlayerID;
unsigned long m_gpID;
unsigned long m_experience;
unsigned char m_level;
};

int _tmain(int argc, _TCHAR* argv[])
{
std::set<Player&> m_sPlayers;
return 0;
}



Error:
c:\program files\microsoft visual studio 10.0\vc\include\xmemory(137): error C2528: “pointer”: 指向引用:
最近需要使用到有序集合,使用了SET。
遇到编译不过的初级问题,代码如下。请帮忙看看,感激不尽,stl set有关问题

#include <set>

class Player
{
public:
Player(const Player& player)
{
this->m_experience = player.m_experience;
this->m_gpID = player.m_gpID;
this->m_level = player.m_level;
this->m_ulPlayerID = player.m_ulPlayerID;
};
Player(const unsigned long playerID, const unsigned long gpID, 
const unsigned long experience, const unsigned char level):
m_ulPlayerID(playerID),m_gpID(m_gpID), 
m_experience(experience), m_level(level){}
bool operator < (const Player &player) const
{
return (this->m_level<player.m_level);
}
Player& operator = (const Player &player)
{
if (&player == this)
{
return (*this);
}
this->m_experience = player.m_experience;
this->m_gpID = player.m_gpID;
this->m_level = player.m_level;
this->m_ulPlayerID = player.m_ulPlayerID;
return (*this);
}
private:
unsigned long m_ulPlayerID;
unsigned long m_gpID;
unsigned long m_experience;
unsigned char m_level;
};

int _tmain(int argc, _TCHAR* argv[])
{
std::set<Player&> m_sPlayers;
return 0;
}


Error:
c:\program files\microsoft visual studio 10.0\vc\include\xmemory(137): error C2528: “pointer”: 指向引用的指针非法
1>          c:\program files\microsoft visual studio 10.0\vc\include\set(25): 参见对正在编译的类 模板 实例化“std::allocator<_Ty>”的引用
1>          with
1>          [
1>              _Ty=Player &
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xtree(451): 参见对正在编译的类 模板 实例化“std::_Tset_traits<_Kty,_Pr,_Alloc,_Mfl>”的引用
1>          with
1>          [
1>              _Kty=Player &,
1>              _Pr=std::less<Player &>,
1>              _Alloc=std::allocator<Player &>,
1>              _Mfl=false

[解决办法]
为什么不用指针呢?
[解决办法]

#include <set>
 
class Player
{
public:
    Player(const Player& player)
    {
        this->m_experience = player.m_experience;
        this->m_gpID = player.m_gpID;
        this->m_level = player.m_level;
        this->m_ulPlayerID = player.m_ulPlayerID;
    };
    Player(const unsigned long playerID, const unsigned long gpID, 
        const unsigned long experience, const unsigned char level):
    m_ulPlayerID(playerID),m_gpID(m_gpID), 
        m_experience(experience), m_level(level){}
    bool operator < (const Player &player) const
    {
        return (this->m_level<player.m_level);
    }
    Player& operator = (const Player &player)
    {
        if (&player == this)


        {
            return (*this);
        }
        this->m_experience = player.m_experience;
        this->m_gpID = player.m_gpID;
        this->m_level = player.m_level;
        this->m_ulPlayerID = player.m_ulPlayerID;
        return (*this);
    }
private:
    unsigned long m_ulPlayerID;
    unsigned long m_gpID;                
    unsigned long m_experience;            
    unsigned char m_level;                
};
 
struct cmp
{
inline bool operator()(const Player* a, const Player*b){ return *a<*b; }
};
 
int main(int argc, char* argv[])
{
    std::set<Player*,cmp> m_sPlayers;
    return 0;
}

热点排行