0

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

void Farm::passTime()
{	
	list<Animal*>::iterator it;

	for(it = farmAnimals.begin(); it != farmAnimals.end(); it++)
	{
		Animal *x = &it;
		&x.Animal::passTime();		
	}
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.