Problem with pointers and iterators

Discussion in 'C++' started by Griff Johns, Oct 6, 2008.

  1. Griff Johns

    Griff Johns Guest

    Hi, if I have the following code:
    class MyObject
    {
    public:
    MyObject() {};
    ~MyObject() {};

    int x;

    }

    class MyObjectList : public std::vector<MyObject> {};

    void Foo(MyObject* obj)
    {
    // Do something with obj->x

    }

    If I loop through it like this:

    MyObjectList list;
    // stuff list with objects

    MyObjectList::iterator it;
    for (it = list.begin(); it != list.end(); ++it) {
    // I used to be able to do this:
    MyObject* p_obj = it;
    Foo(p_obj);

    // Or this
    Foo(it);

    }

    But now the error I get is 'cannot convert parameter 1 from
    'std::vector<_Ty>::iterator' to 'MyObject *'

    This is after migrating a project from vc6.0 to vc7.1, but I think that
    the issue lies with the language standards(?). So why does this happen
    now, and how do I resolve it?

    Thanks.
    Griff Johns, Oct 6, 2008
    #1
    1. Advertising

  2. In article <2008100617014316807-pete@versatilecodingcom>,
    Pete Becker <> wrote:
    [deleted]

    For those baffled by this slew of messages, the original seems to have
    been posted to comp.lang.c++ with followups set to comp.lang.c.
    I'm posting this to both with followups set to comp.lang.c++ so
    that it can be taken up over there.

    -- Richard
    --
    Please remember to mention me / in tapes you leave behind.
    Richard Tobin, Oct 6, 2008
    #2
    1. Advertising

  3. Griff Johns

    Default User Guest

    Griff Johns wrote:

    Why in the hell did you set follow-ups to comp.lang.c?





    Brian
    Default User, Oct 6, 2008
    #3
  4. Griff Johns

    Griff Johns Guest

    What do you mean? I'm posting this to comp.lang.c++ but the only two
    replies have gone on about comp.lang.c for some reason.

    This is a C++ question and I think this is the right place to post it?

    On Mon, 06 Oct 2008 21:59:45 +0000, Default User wrote:
    > Griff Johns wrote:
    >
    > Why in the hell did you set follow-ups to comp.lang.c?
    >
    >
    >
    >
    >
    > Brian
    Griff Johns, Oct 6, 2008
    #4
  5. Griff Johns

    Default User Guest

    From: "Default User" <>
    Subject: Re: Problem with pointers and iterators
    Date: 6 Oct 2008 22:12:59 GMT
    Newsgroups: comp.lang.c
    User-Agent: XanaNews/1.17.5.9

    Griff Johns wrote:

    Don't top-post. Rearranged.

    > On Mon, 06 Oct 2008 21:59:45 +0000, Default User wrote:
    > > Griff Johns wrote:
    > >
    > > Why in the hell did you set follow-ups to comp.lang.c?

    > What do you mean? I'm posting this to comp.lang.c++ but the only two
    > replies have gone on about comp.lang.c for some reason.
    >
    > This is a C++ question and I think this is the right place to post it?


    Here are the full headers from your message:

    From: Griff Johns <>
    Subject: Re: Problem with pointers and iterators
    Date: Mon, 06 Oct 2008 23:05:36 +0100
    Message-ID: <>
    References: <>
    <>
    Lines: 16
    Path: uni-berlin.de!fu-berlin.de!news.germany.com!aioe.org!not-for-mail
    Newsgroups: comp.lang.c++
    Followup-To: comp.lang.c
    Organization: Aioe.org NNTP Server
    NNTP-Posting-Host: N1FvpErgY0u8uU4+C0wjqA.user.aioe.org
    Mime-Version: 1.0
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: 8bit
    X-Complaints-To:
    User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a
    cleansing moment of clarity.)
    Xref: uni-berlin.de comp.lang.c++:1031268



    You have Followup-To set to another newsgroup, and that's where most of
    the replies went.




    Brian
    Default User, Oct 6, 2008
    #5
  6. Griff Johns

    red floyd Guest

    On Oct 6, 1:56 pm, Griff Johns <> wrote:
    > Hi, if I have the following code:
    > class MyObject
    > {
    > public:
    >    MyObject() {};
    >    ~MyObject() {};

    Delete the semicolons after constructor/destructor.
    >
    >    int x;
    >
    > }
    >
    > class MyObjectList : public std::vector<MyObject> {};
    >
    > void Foo(MyObject* obj)
    > {
    >    // Do something with obj->x
    >
    > }
    >
    > If I loop through it like this:
    >
    > MyObjectList list;
    > // stuff list with objects
    >
    > MyObjectList::iterator it;
    > for (it = list.begin(); it != list.end(); ++it) {
    >    // I used to be able to do this:
    >    MyObject* p_obj = it;
    >    Foo(p_obj);
    >
    >    // Or this
    >    Foo(it);
    >
    > }
    >
    > But now the error I get is 'cannot convert parameter 1 from
    > 'std::vector<_Ty>::iterator' to 'MyObject *'
    >
    > This is after migrating a project from vc6.0 to vc7.1, but I think that
    > the issue lies with the language standards(?).  So why does this happen
    > now, and how do I resolve it?



    use Foo(&*it);
    red floyd, Oct 6, 2008
    #6
    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. BCC
    Replies:
    6
    Views:
    4,340
    Peter Koch Larsen
    Dec 1, 2004
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    476
    Kai-Uwe Bux
    May 8, 2005
  3. CBFalconer

    Re: Problem with pointers and iterators

    CBFalconer, Oct 7, 2008, in forum: C Programming
    Replies:
    1
    Views:
    304
    Richard Bos
    Oct 7, 2008
  4. , India
    Replies:
    10
    Views:
    1,060
    James Kanze
    Aug 8, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    654
Loading...

Share This Page