assign const_iterator to const pointer

K

kotau

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
 
E

Earl Purple

kotau said:
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;

What is Item?
name::const_iterator i;

What is name?
name:const_iterator e = p1->end();

Missing a :
What is p1?
for (i=p1->begin(); i<e ++i)

iterators should be compared with operator == and operator !=, not with
operator <
{
if (stricmp(i->func(), var) == 0)
{
p = i;
break;
}
}

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

Because it probably can't and why should it? Iterators do not have to
take assignment to pointers.
I have spent a few hours looking for a definitive answer and have found
nothing so far.

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.
 
K

kotau

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.
 
J

Jim Langston

kotau said:
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;

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top