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

coldplay968 2007-04-27 02:18:12
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
...全文
202 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
coldplay968 2007-04-27
  • 打赏
  • 举报
回复
int i =(*it_).OnLine;
char str[10];
strcpy( str, (*it_).UserId );

知道哪里错了 是(*it_) 少了个 “_”
郁闷,搞了半天,谢谢大家了
coldplay968 2007-04-27
  • 打赏
  • 举报
回复
bool operator ==(const PERSONINFO & rhs)const
{
return ( OnLine == rhs.OnLine && (0==strcmp( 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);

int i =(*it).OnLine;
char str[10];
strcpy( str, (*it).UserId );

改成这个样子就不报错了,但是 里面的值就不对了,明明就有
Info.OnLine = 3;
strcpy( Info.UserId , "a" ); 这个东西啊
晕死

qingcairousi 2007-04-27
  • 打赏
  • 举报
回复
另外find的调用也错了,最后一个参数不应该传地址,应该传对象
qingcairousi 2007-04-27
  • 打赏
  • 举报
回复
编译器说得很清楚了,需要的是二元的operator==,把operator==要么独立出来做成友元,要么做成静态的试试
taodm 2007-04-27
  • 打赏
  • 举报
回复
it_ = find(m_list.begin(), m_list.end(), Info);
coldplay968 2007-04-27
  • 打赏
  • 举报
回复
用了楼上的还是报错
  • 打赏
  • 举报
回复
return ( OnLine == rhs.OnLine && (0==strcmp( UserId ,rhs.UserId)) );
coldplay968 2007-04-27
  • 打赏
  • 举报
回复
bool operator ==(const PERSONINFO & rhs)const
{

return ( OnLine == rhs.OnLine && strcmp( UserId ,rhs.UserId) );
}
改成了这样还是报错啊
taodm 2007-04-27
  • 打赏
  • 举报
回复
你这个也蛮强的UserId == rhs.UserId
一个strcmp
taodm 2007-04-27
  • 打赏
  • 举报
回复
it_ = find(m_list.begin(), m_list.end(), Info);

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧