By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,415 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

assign const_iterator to const pointer

P: n/a
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

Nov 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

kotau wrote:
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.

Nov 9 '06 #2

P: n/a
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.

On Nov 9, 6:00 pm, "Earl Purple" <earlpur...@gmail.comwrote:
kotau wrote:
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.
Nov 9 '06 #3

P: n/a

"kotau" <dj********@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
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);
break;
}
}
}
Someone else wrote the code and I'm trying to fix it...

Cheers.

On Nov 9, 6:00 pm, "Earl Purple" <earlpur...@gmail.comwrote:
>kotau wrote:
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.

Nov 9 '06 #4

P: n/a
Thanks that gets the compile working.

This group rocks!!!

Patrick.

On Nov 9, 6:17 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"kotau" <djcjeve...@gmail.comwrote in messagenews:11**********************@h54g2000cwb.g ooglegroups.com...
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);
break;
}
}
}
Someone else wrote the code and I'm trying to fix it...
Cheers.
On Nov 9, 6:00 pm, "Earl Purple" <earlpur...@gmail.comwrote:
kotau wrote:
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.
Nov 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.