stl中的list问题。
在List中使用通用算法find()时编译错误,请问怎么改?
我只是在List容器中存储了一个结构体,现在想在该容器中查找某一个元素(结构体),就出现这种错误。
BOOL CMainFrame::AddHwndList( HWND hwnd, INT nType )
{
BOOL blRet = FALSE;
CHwndChatList::iterator it = NULL;
ChatHwndStruct hwndStruct = { 0 };
hwndStruct.hwnd = hwnd;
hwndStruct.nType = nType;
// Lock map list.
EnterCriticalSection( &m_lockHwndList );
it = find( m_listChatHwnd.begin(), m_listChatHwnd.end(), hwndStruct );
// 没有查找到,插入新的句柄结构到list第一个节点中。
if ( it != m_listChatHwnd.end() )
{
m_listChatHwnd.push_front( hwndStruct );
blRet = TRUE;
}
else
blRet = FALSE;
// Unlock map list.
LeaveCriticalSection( &m_lockHwndList );
return blRet;
}
Compiling...
Mainfrm.cpp
F:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\algorithm(43) : error C2678: binary '== ' : no operator defined which takes a left-hand operand of type 'struct tagChatHwndStruct ' (or there is no acceptable conversion)
D:\Work\IVTalk\ChatClient\ChatClient\Mainfrm.cpp(985) : see reference to function template instantiation 'class std::list <struct tagChatHwndStruct,class std::allocator <struct tagChatHwndStruct> > ::iterator __cdecl std::find(class std::list <s
truct tagChatHwndStruct,class std::allocator <struct tagChatHwndStruct> > ::iterator,class std::list <struct tagChatHwndStruct,class std::allocator <struct tagChatHwndStruct> > ::iterator,const struct tagChatHwndStruct &) ' being compiled
Error executing cl.exe.
Creating browse info file...
ChatClient.exe - 1 error(s), 0 warning(s)
[解决办法]
提示不是很清楚嘛, hwndStruct结构没有==运算符。
[解决办法]
重载==运算符
[解决办法]
找C++ Primer,看运算符重载。
------解决方案--------------------
no operator defined which takes a left-hand operand of type 'struct tagChatHwndStruct '
[解决办法]
it = find( m_listChatHwnd.begin(), m_listChatHwnd.end(), hwndStruct );
把这个 find 算法自己写吧 ~
不就是搜索链表嘛,
一个 while 循环就可以了,
里面的元素比较可以使用 memcmp 进行.