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