0

I'm a first year of university student , I found myself stuck on a project and sadly I can't find help anywhere so I hope I can find it here.

in my project :

I have a class Person and two classes derived from it (player and trainer)

person has the attributes : name and date of birth. player has the attributes : name and date of birth and position. trainer has the attributes : name and date of birth and licence number.

each attribute has a getter and a setter method;

in the player class I added a method called (show info) that prints out a string containing each of his attributes and did the same for the class trainer .

I have another class called club , in which I have a vector of pointers to person ( I used it because I wanted to add both players and trainers as elements of my vector) which I'm able to do using (push.back)

I have a method called ( addPerson ) that has as a parameter a pointer to person objects , but I need to use it for player and trainer objects.

what I need to do is iterate over all the elements of my vector and call the method (show info) for each of the (players or trainers)

my problem is that I dont know how to specify which of the (show info) methods is to be used , (is it the one in the player class or the trainer class ?) or how to acces the getters of the position or the licence number attributes if I'm able to know that this player is in fact a player or a trainer .

I hope that I described everything well and that I could get some help . thanks for reading.

edit:

I apreciate your answers it really helped a lot , I think I somewhat understood the concept of virtual methods but when I came to apply it it just gives me a random error.

here's my code :

 std::string Club::showInfoMembers(){


       stringstream text;
       std::vector<Person*>::const_iterator iter;

       text << "--------------------" << endl;
       for (iter = m_vMembres.begin(); iter != m_vMembres.end(); iter++){


             text << iter->showinfo());
             text << "--------------------" << endl;

      }

        return text.str();
 }

I get the error :

      request for member ‘showInfo’ in ‘* 
      iter.__gnu_cxx::__normal_iterator<hockey::Person**, 
      std::vector<hockey::Person*> >::operator->()’, which is of pointer type
      ‘hockey::Person*’ (maybe you meant to use ‘->’ ?)

I don't understand what it means.

  • You want `showInfo` to be declared `virtual`: see e.g.: https://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c – UnholySheep Apr 28 '20 at 22:26
  • @UnholySheep thanks for the suggestion , I tried that but I get a random error I can't seem to understand – Abderrahmane Bouhya Apr 28 '20 at 22:54
  • @SamVarshavchik I'm aware that this website is not meant for that but I just tried my best , I did fetch all around my textbooks and project guide maybe I just need to try harder , thanks for the advice – Abderrahmane Bouhya Apr 28 '20 at 22:58

1 Answers1

0

You're almost there. (Assuming Person has a virtual showInfo method and your Player and Trainer classes implement their respective versions). The problem is in your iterator loop. The variable iter is of type std::vector<Person*>::const_iterator not Person* so this line does not work.

   text << iter->showinfo();

because the iterator does not know about showInfo(). To get at the contents of the iterator you need to dereference it as shown below.

   text << (*iter)->showinfo();

Hope this helps!

idz
  • 12,825
  • 1
  • 29
  • 40