473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with pointers and iterators

BCC
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,
Bryan
Jul 22 '05 #1
6 1635
BCC wrote:
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?


An iterator is not a pointer. If you need to convert the iterator value
into a pointer to an object behind the iterator, you need to dereference
it first and then take the address:

MyObject* p_obj = &*it;

V
Jul 22 '05 #2
BCC
Victor Bazarov wrote:
BCC wrote:
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?

An iterator is not a pointer. If you need to convert the iterator value
into a pointer to an object behind the iterator, you need to dereference
it first and then take the address:

MyObject* p_obj = &*it;

V


I knew that an iterator was not a pointer, but I see that I was sloppy
and vc6.0 let me get away with it. Wonder what changed between the two
versions...

Thanks!
Bryan
Jul 22 '05 #3
BCC wrote:

I knew that an iterator was not a pointer, but I see that I was sloppy
and vc6.0 let me get away with it. Wonder what changed between the two
versions...


An implmenetation can implement vector::iterator as a pointer if it wants.
It will work fine. However, it can also impemenet it as a class (most likely
containing a pointer). The advantage of the latter is that it avoids
bogus conversions of the iterator to other pointers.
Jul 22 '05 #4
BCC wrote:
[...]
I knew that an iterator was not a pointer, but I see that I was sloppy
and vc6.0 let me get away with it. Wonder what changed between the two
versions...


The implementation of the Standard library, apparently.

To make sure you don't rely on your compiler and library implementation
details in your project I strongly recommend you to use more than one
compiler at a time. Of course some code you write will have to use
platform-specific elements, like OS calls and GUI, but if you isolate
those in a particular location, the rest of the code can easily be
compiled with another compiler.

V
Jul 22 '05 #5
BCC wrote:
Hi, if I have the following code:

class MyObject {
public:
MyObject(void) { };
~MyObject(void) { };
private:
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;
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?


The standard specifies that
`*it' is a reference to an object of type MyObject
but `it' is not necesaarily a pointer to that object.
Jul 22 '05 #6
Hi BCC

As others have pointed out, it is not necessarily a pointer so using it as
one is illegal (use &*it).
I will add that your Foo function looks odd. If Foo expects a pointer to a
valid Foo object, Foo should use a call by reference (void Foo(MyObject&)),
and if Foo only needs to manipulate obj->x, Foo should actually expect an
object of that type - and not a MyObject.
[snip]
/Peter
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Gianni Mariani | last post by:
I'm looking at templatizing some code that uses pointers and iterators. There are 3 kinds of values for pointers. - Null - Invalid ( the value is ((T*)-1) ) - Valid Null indicates that...
4
by: Leon | last post by:
Hi all. I'm a bit confused about the use of STL iterators and pointers, and I was wondering if maybe you could give me some pointers ;) Is it possible to convert between iterators and pointers...
2
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
6
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
8
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
8
by: Steve Lambert | last post by:
Hi, I'd be grateful if someone could clarify this for me. In the linked list structure my intention is to declare an array of length 3 containing pointers to node eg. Node *Iterators The...
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
15
by: fungus | last post by:
I'm moving some code from VC++ 6 to VC++ 2005 and I've run into a nasty problem because iterators are no longer pointers. In the program I'm moving, there's a std::vector of items hidden inside...
8
by: ZikO | last post by:
Hi Are iterators' behaviour the same as pointers? I mean I know i can go through the container with the same manner as if i use pointers (ptr++) and i can derefference iterators the same way. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.