Problem with pointers and iterators

G

Griff Johns

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

Richard Tobin

[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
 
D

Default User

Griff Johns wrote:

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





Brian
 
G

Griff Johns

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?
 
D

Default User

From: "Default User" <[email protected]>
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.
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 <[email protected]>
Subject: Re: Problem with pointers and iterators
Date: Mon, 06 Oct 2008 23:05:36 +0100
Message-ID: <[email protected]>
References: <[email protected]>
<[email protected]>
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: (e-mail address removed)
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
 
R

red floyd

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

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

Similar Threads

Problem with pointers and iterators 6
What kind of pattern is this? 15
Strange bug with iterators 3
sorts and iterators 10
Subtyping iterators 5
iterators 16
plain iterators and reverse iterators on vector 10
iterators 0

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top