assign const_iterator to const pointer

Discussion in 'C++' started by kotau, Nov 9, 2006.

  1. kotau

    kotau Guest

    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
    kotau, Nov 9, 2006
    #1
    1. Advertising

  2. kotau

    Earl Purple Guest

    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.
    Earl Purple, Nov 9, 2006
    #2
    1. Advertising

  3. kotau

    kotau Guest

    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" <> wrote:
    > 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.
    kotau, Nov 9, 2006
    #3
  4. kotau

    Jim Langston Guest

    "kotau" <> wrote in message
    news:...
    > 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" <> wrote:
    >> 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.

    >
    Jim Langston, Nov 9, 2006
    #4
  5. kotau

    kotau Guest

    Thanks that gets the compile working.

    This group rocks!!!

    Patrick.

    On Nov 9, 6:17 pm, "Jim Langston" <> wrote:
    > "kotau" <> wrote in messagenews:...
    >
    >
    >
    > > 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" <> wrote:
    > >> 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.
    kotau, Nov 9, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. coala
    Replies:
    3
    Views:
    360
    coala
    Sep 6, 2006
  2. coala
    Replies:
    1
    Views:
    576
    Victor Bazarov
    Sep 6, 2006
  3. Replies:
    11
    Views:
    1,084
  4. Javier
    Replies:
    2
    Views:
    540
    James Kanze
    Sep 4, 2007
  5. Disc Magnet
    Replies:
    1
    Views:
    615
    Ian Collins
    May 6, 2010
Loading...

Share This Page