0

I have the following code:

#include<bits/stdc++.h>

using namespace std;

class Player{
  public:
      string name;
      int s, t;
      Player(string name, int s, int t){this->name=name, this->s=s, this->t=t;}

      virtual int getHability() = 0;
};

class PlayerType1: Player{
  public:
  PlayerType1(string name, int s, int t):Player(name, s, t){};

  int getHability(){return this->s+2*this->t;}
};

class PlayerType2: Player{
  public:
  PlayerType2(string name, int s, int t):Player(name, s, t){};

  int getHability(){return 2*this->s+this->t;}
};

class Team{

  vector<unique_ptr<Player>> players;

  Team(const Team& team){
    /*problem here!!*/
  }
};

int main(){

  return 0;
}

Basically I have an abstract class that represents a player and derived class of specific types of players (striker, defender).

I also have a class of team and I'm storing the players of that team using std::unique_ptr pointing to my Player class.

My problem arises when I try to create a copy constructor in the Team class and have to pass the vector of unique pointers from one class to another. Since I'm using a unique pointer I would have to dereference it to copy the vector (based on this). But when I try to deference it, the compiler gives me an error since it is an abstract class. Is there any possible way to do it?

I'm open to suggestions on another way to store the players if my method is not correct.

Bruno Mello
  • 3,751
  • 1
  • 5
  • 27
  • 1
    Does [this question](https://stackoverflow.com/q/5148706/501250) sound like what you're trying to do? – cdhowie Jun 18 '20 at 21:26
  • another target: https://stackoverflow.com/q/34797849/4342498 – NathanOliver Jun 18 '20 at 21:26
  • @cdhowie I think that solves it, thanks! – Bruno Mello Jun 18 '20 at 21:28
  • @BrunoMello Perfect, I will close this question as a duplicate. Feel free to open any other questions if you run into further issues. – cdhowie Jun 18 '20 at 21:29
  • @NathanOliver I have seen that one but it gave me an error because it dereferences the pointer – Bruno Mello Jun 18 '20 at 21:29
  • 1
    Unrelated: be careful with `#include` ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) and `using namespace std;` ([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) . Be really careful when using the two together. They turn the global namespace into a minefield of identifiers you have to avoid. – user4581301 Jun 18 '20 at 21:37

0 Answers0