Problem accessing base class member function whilst iterating through STL list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 1
Reputation: mancmanomyst is an unknown quantity at this point 
Solved Threads: 0
mancmanomyst mancmanomyst is offline Offline
Newbie Poster

Problem accessing base class member function whilst iterating through STL list

 
0
  #1
May 20th, 2008
Hello Everyone - I'm sorry if iasking a repeat question but i've searched the forums and cant find out what's going wrong.

I am writing a program that has a pure virtual base animal class that has three subclasses - Cow, Chicken and Sheep.

Objects of the three types are created and stored in an STL list of type Animal but to avoid slicing problems instead of storing the actual animal in the list its an Animal*.

In my Animal base class I have a member function passTime() that increments the int age of the animal object be it a chicken/cow or sheep.

I have a method in my farm class (which stores the list of animals) also called passTime and this method iterates through the list of animals and executes the Animal passTime function on each of the objects.

However i'm getting C2440 and C2228 errors. I think it's something to do with the iterator and casting but i'm new to c++ and it's confusing me

Here is the function in question

  1. void Farm::passTime()
  2. {
  3. list<Animal*>::iterator it;
  4.  
  5. for(it = farmAnimals.begin(); it != farmAnimals.end(); it++)
  6. {
  7. Animal *x = &it;
  8. &x.Animal::passTime();
  9. }
  10. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem accessing base class member function whilst iterating through STL list

 
0
  #2
May 20th, 2008
it is a pointer
Animal *x = *it;
x->passTime();
Last edited by Ancient Dragon; May 20th, 2008 at 10:37 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 853 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC