Hi,
If i iterate over a list of pointers to objects, how can i access a member function of an object pointed by a pointer from within the list?
So, suppose:
list<Myclass*> my_list
list<Myclass*>::iterator it;
for (it = my_list.begin() ; it != my_list.end() ; it++) {
cout << (*it)->get_name() << endl;
}
Suppose i added some object pointers in my list, why does this for loop print nothing?
Thanks.
9 replies to this topic
#1
Posted 17 December 2011 - 09:16 AM
|
|
|
#2
Posted 17 December 2011 - 09:25 AM
Without knowing what Myclass is, or whether you've added names to the items in the list, it's REALLY hard to know.
#3
Posted 17 December 2011 - 09:37 AM
Myclass would be like this:
class Myclass {
public:
Myclass(string name) : name_(name) {}
string get_name () { return name_ }
private:
string name_
};
#4
Posted 17 December 2011 - 09:48 AM
Standard request: please provide complete, compilable code that demonstrates the problem.
#5
Posted 17 December 2011 - 10:30 AM
This should output "feed 1" i think, but it doesnt.
Here you go:
Here you go:
#include <iostream>
#include <stdlib.h>
#include <list>
#include <dirent.h>
using namespace std;
class Feed {
public:
Feed(string name ="") : feedname(name) {
feedpath = ".//feeds//";
}
virtual ~Feed() {
}
string get_feedname() {
string name = feedname;
return name;
}
string get_feedpath() {
return feedpath;
}
string feedpath;
string feedname;
};
class LocalFeed: public Feed {
public:
LocalFeed(string name = "") : Feed(name) {}
};
class UrlFeed: public Feed {
public:
UrlFeed(string name = "", string url = "") : Feed(name) {
feedurl = url;
}
string get_feedurl() {
return feedurl;
}
private:
string feedurl;
};
list<Feed*> mylist;
void put_in_list(Feed* my) { // steek 1 feed in de myfeeds lijst
mylist.push_back(my);
}
Feed* lookup() {
list<Feed*>::iterator it;
for (it = mylist.begin(); it != mylist.end(); it++) {
if ((*it)->get_feedname() == "feed 1")
return *it;
}
return NULL;
}
int main() {
string feedspath = ".//feeds"; // suppose you have a map "feeds" with other submaps "feed 1" "feed 2" ...
DIR *dir = opendir(feedspath.c_str());
struct dirent *dp;
while (dp = readdir(dir), dp != NULL) {
string name = dp->d_name;
if (name.length() > 4) {
string copypath = feedspath;
copypath.append("//");
string feedpath = copypath.append(name);
LocalFeed myfeed(name);
LocalFeed * myfeedpointer = &myfeed;
put_in_list(myfeedpointer);
}
}
closedir(dir);
Feed* x = lookup();
cout << x->get_feedname() << endl;
return 0;
}
#6
Posted 17 December 2011 - 10:44 AM
First observation: with all the stuff going on with feeds (which I don't have on my system), you are not isolating whether the problem is with reading a feed, or with outputting the result of the method. Can you create a dummy version that doesn't rely on the file system?
#7
Posted 17 December 2011 - 10:53 AM
Well if i replace the main with this, it works, but i can't figure out why the first main doesn't work...
int main() {
LocalFeed a("feed 1");
LocalFeed b("feed 2");
LocalFeed * apointer = &a;
LocalFeed * bpointer = &b;
put_in_list(apointer);
put_in_list(bpointer);
Feed* x = lookup();
cout << x->get_feedname() << endl;
return 0;
}
#8
Posted 17 December 2011 - 01:54 PM
Are you sure your loop is finding names? the .// looks AWEFULLY suspicious.
#9
Posted 17 December 2011 - 02:01 PM
yes, .// is for linux root directory, but i found out what's wrong a minute ago, the problem was that this:
...should have been done with the new operator, since my object is destroyed after i jump out of the if, but still my pointer remains, so dangling pointers was the problem.
correct:
LocalFeed myfeed(name); LocalFeed * myfeedpointer = &myfeed; put_in_list(myfeedpointer)
...should have been done with the new operator, since my object is destroyed after i jump out of the if, but still my pointer remains, so dangling pointers was the problem.
correct:
LocalFeed* myfeedpointer = new LocalFeed (name); put_in_list(myfeedpointer);thanks for your time Winged.
#10
Posted 18 December 2011 - 06:06 AM
I'm glad I could help.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









