Jump to content

iterating lists c++

- - - - -

  • Please log in to reply
9 replies to this topic

#1
ZxiFer

ZxiFer

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Without knowing what Myclass is, or whether you've added names to the items in the list, it's REALLY hard to know.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ZxiFer

ZxiFer

    Newbie

  • Members
  • PipPip
  • 16 posts
Myclass would be like this:

class Myclass {

  public:

    Myclass(string name) : name_(name) {}

    string get_name () { return name_ }

  private:

    string name_

};


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Standard request: please provide complete, compilable code that demonstrates the problem.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
ZxiFer

ZxiFer

    Newbie

  • Members
  • PipPip
  • 16 posts
This should output "feed 1" i think, but it doesnt.

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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
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?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
ZxiFer

ZxiFer

    Newbie

  • Members
  • PipPip
  • 16 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Are you sure your loop is finding names? the .// looks AWEFULLY suspicious.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
ZxiFer

ZxiFer

    Newbie

  • Members
  • PipPip
  • 16 posts
yes, .// is for linux root directory, but i found out what's wrong a minute ago, the problem was that this:

	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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I'm glad I could help.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users