| | |
Problem accessing base class member function whilst iterating through STL list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 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
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
C++ Syntax (Toggle Plain Text)
void Farm::passTime() { list<Animal*>::iterator it; for(it = farmAnimals.begin(); it != farmAnimals.end(); it++) { Animal *x = ⁢ &x.Animal::passTime(); } }
Re: Problem accessing base class member function whilst iterating through STL list
0
#2 May 20th, 2008
![]() |
Other Threads in the C++ Forum
- Previous Thread: Sense changes of a File
- Next Thread: fun, but hard work
Views: 853 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






