Problem with pointers and iterators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Griff Johns

    Problem with pointers and iterators

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

    int x;

    }

    class MyObjectList : public std::vector<MyO bject{};

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

    }

    If I loop through it like this:

    MyObjectList list;
    // stuff list with objects

    MyObjectList::i terator 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<_T y>::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.

  • Richard Tobin

    #2
    Re: Problem with pointers and iterators

    In article <20081006170143 16807-pete@versatilec odingcom>,
    Pete Becker <pete@versatile coding.comwrote :
    [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.

    Comment

    • Default User

      #3
      Re: Problem with pointers and iterators

      Griff Johns wrote:

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





      Brian

      Comment

      • Griff Johns

        #4
        Re: Problem with pointers and iterators

        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

        Comment

        • red floyd

          #5
          Re: Problem with pointers and iterators

          On Oct 6, 1:56 pm, Griff Johns <n...@spam.comw rote:
          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<MyO bject{};
          >
          void Foo(MyObject* obj)
          {
             // Do something with obj->x
          >
          }
          >
          If I loop through it like this:
          >
          MyObjectList list;
          // stuff list with objects
          >
          MyObjectList::i terator 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<_T y>::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);

          Comment

          Working...