Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Iterator fun

22 views
Skip to first unread message

Derwin Crummel

unread,
Jan 31, 2016, 6:56:17 PM1/31/16
to
Found this function in application code for a NNTP client. Errors are
marked below. Clearly, the iterator is mis-applied here but how to
fix?

int TArticleBank::CreateArticleIndex (TArticleIndexList * pIndexLst)
{
ASSERT( pIndexLst );

CSingleLock sLock(&m_crit, TRUE);

// RLW - only call PuntLinkInformation if there are articles to
move to m_setNodes
if (GetCount() > 0)
{
// move m_Stage to m_setNodes
PuntLinkInformation ();
}

// Note: flat list is created during Link(). So go directly(!) to
// the m_setNodes

STLMapArtNode::iterator it = m_setNodes.begin();

while (it != m_setNodes.end() )
{
TArtNode * pNode = *it++; //error C2440

ASSERT(pNode);

pNode->CreateArticleIndex ( pIndexLst );

}
return 0;
}

//-------------------------------------------------------------------------
int TArticleBank::Empty ()
{
CSingleLock sLock(&m_crit, TRUE);

ASSERT(m_Stage.size() == 0);
m_Stage.clear();

STLMapArtNode::iterator it = m_setNodes.begin();

while (it != m_setNodes.end())
{
TArtNode * pNode = *it; //error C2440

// delete call (1/3)
delete pNode;
it++;
}
m_setNodes.erase (m_setNodes.begin(), m_setNodes.end());

m_ids.Empty();

delete m_pMyViewFilter;
m_pMyViewFilter = 0;

return 0;
}

error C2440: 'initializing' : cannot convert from 'const
ob_artnode_ptr' to 'TArtNode *'

Victor Bazarov

unread,
Jan 31, 2016, 7:32:03 PM1/31/16
to
On 1/31/2016 6:56 PM, Derwin Crummel wrote:
> Found this function in application code for a NNTP client. Errors are
> marked below. Clearly, the iterator is mis-applied here but how to
> fix?
>
> int TArticleBank::CreateArticleIndex (TArticleIndexList * pIndexLst)
> {
> ASSERT( pIndexLst );
>
> CSingleLock sLock(&m_crit, TRUE);
>
> // RLW - only call PuntLinkInformation if there are articles to
> move to m_setNodes
> if (GetCount() > 0)
> {
> // move m_Stage to m_setNodes
> PuntLinkInformation ();
> }
>
> // Note: flat list is created during Link(). So go directly(!) to
> // the m_setNodes
>
> STLMapArtNode::iterator it = m_setNodes.begin();
>
> while (it != m_setNodes.end() )
> {
> TArtNode * pNode = *it++; //error C2440
>
> ASSERT(pNode);
>
> pNode->CreateArticleIndex ( pIndexLst );

A set (if 'm_setNodes' is declared as 'std::set') contains *constant*
objects. Once they are inserted, they are not subject to any mutation.

You can pull it out, mutate it as you please, then reinsert it.

>
> }
> return 0;
> }
>
> //-------------------------------------------------------------------------
> int TArticleBank::Empty ()
> {
> CSingleLock sLock(&m_crit, TRUE);
>
> ASSERT(m_Stage.size() == 0);
> m_Stage.clear();
>
> STLMapArtNode::iterator it = m_setNodes.begin();
>
> while (it != m_setNodes.end())
> {
> TArtNode * pNode = *it; //error C2440

Same idea. You can't get a mutable object that retains its position in
the set. If you need to remove it, then remove it (using .erase(it)),
get the iterator that it returns, and then do what you want with the
pointer.

Perhaps you should consider storing smart pointers in that set. When
removed from the set, they will destroy the object they point to.

>
> // delete call (1/3)
> delete pNode;
> it++;
> }
> m_setNodes.erase (m_setNodes.begin(), m_setNodes.end());
>
> m_ids.Empty();
>
> delete m_pMyViewFilter;
> m_pMyViewFilter = 0;
>
> return 0;
> }
>
> error C2440: 'initializing' : cannot convert from 'const
> ob_artnode_ptr' to 'TArtNode *'
>

V
--
I do not respond to top-posted replies, please don't ask
0 new messages