Simple iterator question

M

michael

Hi All,

I have :

#include <iostream>
#include <list>
using std::cout;
using std::list;
using std::endl;

class MyClass {
private:
int myVar;
public:
MyClass(int v):myVar(v){};
int getVar(){return myVar;};
void setVar(int var){myVar = var;};
};

void someFunc(Not sure what to put here c){}

int main(){
list<MyClass> myList;
list<MyClass>::iterator it;

for(int i = 0; i < 5; i++){
MyClass element(i);
myList.push_front(element);
}

for(it = myList.begin(); it != myList.end(); it++){
if(*it.getVar() == 2){
//someFunc(it);
}
}
cout << "done" << endl;
}

(I know I have some magic numbers, please ignore these, I am just trying to
get the iterator stuff clear in my head.)

I have two questions:
1. Why can I not access the members of MyClass using *it.memb_name()? If I
make it a list of MyClass* I can use (*it)->memb_name(), so why when I have
a list of MyClass does *it.getVar() cause the compiler to complain thet
getVar() is undeclared?

2. How (can I?) pass the iterator to another function? What prototype would
I use?

Thanks for your help

Regards

Michael
 
J

Jim Langston

michael said:
Hi All,

I have :

#include <iostream>
#include <list>
using std::cout;
using std::list;
using std::endl;

class MyClass {
private:
int myVar;
public:
MyClass(int v):myVar(v){};
int getVar(){return myVar;};
void setVar(int var){myVar = var;};
};

void someFunc(Not sure what to put here c){}

int main(){
list<MyClass> myList;
list<MyClass>::iterator it;

for(int i = 0; i < 5; i++){
MyClass element(i);
myList.push_front(element);
}

for(it = myList.begin(); it != myList.end(); it++){
if(*it.getVar() == 2){
//someFunc(it);
}
}
cout << "done" << endl;
}

(I know I have some magic numbers, please ignore these, I am just trying
to
get the iterator stuff clear in my head.)

I have two questions:
1. Why can I not access the members of MyClass using *it.memb_name()? If I
make it a list of MyClass* I can use (*it)->memb_name(), so why when I
have
a list of MyClass does *it.getVar() cause the compiler to complain thet
getVar() is undeclared?

Operator precidence. Try
(*it).memb_name();
2. How (can I?) pass the iterator to another function? What prototype
would
I use

I think
void MyFunc( std::list<MyClass>::iterator it )
should work, but have never tried.
 
R

red floyd

michael said:
Hi All,
[redacted]

int main(){
list<MyClass> myList;
list<MyClass>::iterator it;

for(int i = 0; i < 5; i++){
MyClass element(i);
myList.push_front(element);
}

for(it = myList.begin(); it != myList.end(); it++){
Prefer the prefix form of increment: ++it
if(*it.getVar() == 2){
//someFunc(it);
}
}
cout << "done" << endl;
}

(I know I have some magic numbers, please ignore these, I am just trying to
get the iterator stuff clear in my head.)

I have two questions:
1. Why can I not access the members of MyClass using *it.memb_name()?
Look up the releative precedences of unary operator* and operator. Use
either (*it).memb_name(), or it->memb_name().
2. How (can I?) pass the iterator to another function? What prototype would
I use?

void f(list<MyClass>::iterator);

for (it = myList.begin(); it != myList.end(); ++it)
f(it);
 
C

Christopher Pisz

michael said:
Hi All,

I have :

#include <iostream>
#include <list>
using std::cout;
using std::list;
using std::endl;

class MyClass {
private:
int myVar;
public:
MyClass(int v):myVar(v){};
int getVar(){return myVar;};
void setVar(int var){myVar = var;};
};

void someFunc(Not sure what to put here c){}

int main(){
list<MyClass> myList;
list<MyClass>::iterator it;

for(int i = 0; i < 5; i++){
MyClass element(i);
myList.push_front(element);
}

for(it = myList.begin(); it != myList.end(); it++){
if(*it.getVar() == 2){
//someFunc(it);
}
}
cout << "done" << endl;
}

(I know I have some magic numbers, please ignore these, I am just trying
to
get the iterator stuff clear in my head.)

I have two questions:
1. Why can I not access the members of MyClass using *it.memb_name()? If I
make it a list of MyClass* I can use (*it)->memb_name(), so why when I
have
a list of MyClass does *it.getVar() cause the compiler to complain thet
getVar() is undeclared?

2. How (can I?) pass the iterator to another function? What prototype
would
I use?

Thanks for your help

Regards

Michael



1)
Did you try it->memb_name?
Or (*it).member_name?
*it.member_name doesn't work because you are trying to dereference the whole
thing.
Same reason you had to use parens with the list of pointers, where it was
(*it)->member_name;

2)
why not?
void someFunc( list<MyClass>::iterator 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

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top