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

模板函数find这样做为什么有错,该如何解决!

2012-03-11 
模板函数find这样做为什么有错,该怎么解决!!!!!!!!!!!!structPERSONINFO{intOnLinecharUserId[10]boolop

模板函数find这样做为什么有错,该怎么解决!!!!!!!!!!!!
struct         PERSONINFO
{
int       OnLine;
char     UserId[10];
bool   operator==(const   PERSONINFO   &   rhs)const
{
        return   (   OnLine   ==   rhs.OnLine   &&   UserId   ==   rhs.UserId);
}
};

PERSONINFO       pInfo   ;
pInfo.OnLine   =   3;
strcpy(   pInfo.UserId   ,   "a "   );
m_list.push_back(pInfo);

pInfo.OnLine   =   4;
strcpy(   pInfo.UserId   ,   "b "   );
m_list.push_back(pInfo);

pInfo.OnLine   =   1;
strcpy(   pInfo.UserId   ,   "c "   );
m_list.push_back(pInfo);

PERSONINFO       Info   ;

Info.OnLine   =   3;
strcpy(   Info.UserId   ,   "a "   );

list <PERSONINFO> ::iterator   it_;
it_   =   find(m_list.begin(),   m_list.end(),   &Info);

报错:d:\microsoft   visual   studio\vc98\include\algorithm(43)   :   error   C2678:   binary   '== '   :   no   operator   defined   which   takes   a   left-hand   operand   of   type   'struct   PERSONINFO '   (or   there   is   no   acceptable   conversion)

                E:\SortList\SortListDlg.cpp(258)   :   see   reference   to   function   template   instantiation   'class   std::list <struct   PERSONINFO,class   std::allocator <struct   PERSONINFO>   > ::iterator   __cdecl   std::find(class   std::list <struct   PERSONINFO,class   std::allocat
or <struct   PERSONINFO>   > ::iterator,class   std::list <struct   PERSONINFO,class   std::allocator <struct   PERSONINFO>   > ::iterator,struct   PERSONINFO   *const   &   ) '   being   compiled

[解决办法]
it_ = find(m_list.begin(), m_list.end(), Info);

[解决办法]
编译器说得很清楚了,需要的是二元的operator==,把operator==要么独立出来做成友元,要么做成静态的试试

热点排行