assign const_iterator to const pointer

assign const_iterator to const pointer

Post by kota » Fri, 10 Nov 2006 19:48:59


Hi,

I'm having trouble with something that would appear to have a simple
solution.

Here's a version of the code I'm working with:


const Item* p 0;
name::const_iterator i;
name:const_iterator e = p1->end();
for (i=p1->begin(); i<e ++i)
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}

The compiler returns that it "cannot convert from const_iterator to
const(Item*)"


I have spent a few hours looking for a definitive answer and have found
nothing so far.

Any help will be greatly appreciated.

Cheers.

Patrick
 
 
 

assign const_iterator to const pointer

Post by Earl Purpl » Fri, 10 Nov 2006 20:00:40


What is Item?


What is name?


Missing a :
What is p1?


iterators should be compared with operator == and operator !=, not with
operator <


Because it probably can't and why should it? Iterators do not have to
take assignment to pointers.


The answer lies not in how you get your iterator to assign to a pointer
but to how to manage to do what you need to do without it.

 
 
 

assign const_iterator to const pointer

Post by kota » Fri, 10 Nov 2006 20:10:43

Hi,

Thanks for your reply. Sorry for the syntax errors above.

Here's a more complete version:

static const Item* somefunc(const name* p1, const char* var)
{

const Item* p = 0;
name::const_iterator i;
name::const_iterator e = p1->end();
for (i=p1->begin(); i<e ++i)
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}
}


Someone else wrote the code and I'm trying to fix it...

Cheers.
 
 
 

assign const_iterator to const pointer

Post by Jim Langst » Fri, 10 Nov 2006 20:17:54


p is an Item*, i is a const_iterator. They are not assignable. The way to
convert an iterator to a pointer is to dereference the iterator, then take
the address of it. Try:
p = &(*i);
 
 
 

assign const_iterator to const pointer

Post by kota » Fri, 10 Nov 2006 20:24:01

Thanks that gets the compile working.

This group rocks!!!

Patrick.