Iterator and arrays

P

pmatos

Hi all,

I've a problem I'm not able to solve with iterators and to examplify
the problem I've created a minimal program which results in the same
error:
#include <iostream>
#include <list>

using namespace std;

struct _foo {
int a;
};

typedef struct _foo foo;

int main() {

list<foo*> * var;

var = new list<foo*>[5];

for(int i = 0; i < 5; i++)
for(list<foo*>::const_iterator it = var.begin(); it !=
var.end(); it++)
cout << it->a << endl;

delete[] var;

return 0;

}

What's the problem with it->a?

I get from g++:
test.cc: In function `int main()':
test.cc:20: error: request for member `a' in
`*(&it)->std::_List_iterator<_Tp,
_Ref, _Ptr>::eek:perator-> [with _Tp = foo*, _Ref = foo* const&, _Ptr =
foo*
const*]()', which is of non-class type `foo* const'

Can't understand the issue...
Can somebody help?

Cheers,

Paulo Matos
 
G

Gianni Mariani

pmatos said:
Hi all,

I've a problem I'm not able to solve with iterators and to examplify
the problem I've created a minimal program which results in the same ....
for(list<foo*>::const_iterator it = var.begin(); it !=
var.end(); it++)
cout << it->a << endl;


cout << (*it)->a << endl;

(*it) evaluates to a foo* - you have a double indirection here.
 
V

Victor Bazarov

pmatos said:
I've a problem I'm not able to solve with iterators and to examplify
the problem I've created a minimal program which results in the same
error:
#include <iostream>
#include <list>

using namespace std;

struct _foo {
int a;
};

typedef struct _foo foo;

Don't do that, please. Just name your struct 'foo' and move on.
int main() {

list<foo*> * var;

var = new list<foo*>[5];

So, every var[j] is a whole list.
for(int i = 0; i < 5; i++)
for(list<foo*>::const_iterator it = var.begin(); it !=
var.end(); it++)
cout << it->a << endl;

delete[] var;

return 0;

}

What's the problem with it->a?


That's simple. Or maybe not that simple, but still.

What is 'it'? 'it' is an iterator. What will operator-> for an iterator
return? The referenced object. What type is that object? A pointer to
'foo'. Does it have a member 'a'? No. A pointer to 'foo' does NOT have
any members. 'foo' does, however. So, how do you get a 'foo' from
a pointer to 'foo'? You dereference.

Using -> with iterators to standard containers make sense only if your
container stores objects. If it stores pointers, you need to dereference
the iterator first and only then use the -> on that pointer:

cout said:
I get from g++:
test.cc: In function `int main()':
test.cc:20: error: request for member `a' in
`*(&it)->std::_List_iterator<_Tp,
_Ref, _Ptr>::eek:perator-> [with _Tp = foo*, _Ref = foo* const&, _Ptr =
foo*
const*]()', which is of non-class type `foo* const'

Can't understand the issue...
Can somebody help?

See above.

V
 
P

pmatos

Victor said:
Don't do that, please. Just name your struct 'foo' and move on.

Could you please explain why? I don't feel confortable writing 'struct
foo' all over my code.
int main() {

list<foo*> * var;

var = new list<foo*>[5];

So, every var[j] is a whole list.

I don't understand that...
for(int i = 0; i < 5; i++)
for(list<foo*>::const_iterator it = var.begin(); it !=
var.end(); it++)
cout << it->a << endl;

delete[] var;

return 0;

}

What's the problem with it->a?


That's simple. Or maybe not that simple, but still.

What is 'it'? 'it' is an iterator. What will operator-> for an iterator
return? The referenced object. What type is that object? A pointer to
'foo'. Does it have a member 'a'? No. A pointer to 'foo' does NOT have
any members. 'foo' does, however. So, how do you get a 'foo' from
a pointer to 'foo'? You dereference.

Using -> with iterators to standard containers make sense only if your
container stores objects. If it stores pointers, you need to dereference
the iterator first and only then use the -> on that pointer:

cout << (*it)->a << endl;


Damn, that's so stupid of me... Indeed you are correct. I did try *it->
but it strikes me however to know that -> has higher precedence than *.
Well, at least I learned something new... ;)
I get from g++:
test.cc: In function `int main()':
test.cc:20: error: request for member `a' in
`*(&it)->std::_List_iterator<_Tp,
_Ref, _Ptr>::eek:perator-> [with _Tp = foo*, _Ref = foo* const&, _Ptr =
foo*
const*]()', which is of non-class type `foo* const'

Can't understand the issue...
Can somebody help?

See above.

Thanks,

Paulo Matos
 
V

Victor Bazarov

pmatos said:
Could you please explain why? I don't feel confortable writing 'struct
foo' all over my code.

In C++ you don't need to. Perhaps you should begin by unlearning bad
C habits you might have.
[...]
cout << (*it)->a << endl;

Damn, that's so stupid of me... Indeed you are correct. I did try *it->
but it strikes me however to know that -> has higher precedence than *.
Well, at least I learned something new... ;)

Do a little bit of that every day and you may even grow to like it :)

V
 
M

Matthias Kaeppler

pmatos said:
Could you please explain why? I don't feel confortable writing 'struct
foo' all over my code.

I think Victor was referring to 17.4.3.1.2 of the ISO C++ standard:

"Certain sets of names and function signatures are always reserved to
the implementation: Each name that contains a double underscore (_ _)
or begins with an underscore followed by an uppercase letter (2.11) is
reserved to the implementation for any use."

"Each name that begins with an underscore is reserved to the
implementation for use as a name in the global namespace.165)"

That means, just call your struct foo, and not _foo, because the name
_foo is reserved for the implementation.
 
R

Rolf Magnus

pmatos said:
Could you please explain why?

Because there is no reason to, and _foo is a reserved name.
I don't feel confortable writing 'struct foo' all over my code.

Then don't. You don't need to. This is C++, not C.
 

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

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top