dimanche 22 janvier 2017

Why did I get the: "Attempting to reference a deleted function" error after adding a vector with unique pointers to my header file?

To learn C++ I am programming a game. In this game, there can be two players. Those players, are able to connect to each other with sockets. After adding a vector with unique pointers to the player header I got the error: "Error C2280 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function". Same error goes for the Building vectors.

To give a more clear view of what the problem is, I have set my header file in a code block below:

class Player {
public:
    Player() {}
    Player(const std::string& name) : _name {name} {}

    std::string get_name() const { return _name; }
    void set_name(const std::string & new_name) { _name = new_name; }

    const bool compare_name(const std::string & name) const;
    void set_coins(const int coins);
    const int get_coins();
    void set_king(const bool king);
    const bool get_king();

private:
    std::string _name;
    int _coins;
    bool _is_king;

    std::vector<std::unique_ptr<Building>> _buildings;
    std::vector<std::unique_ptr<Building>> _placed_buildings;
    std::vector<std::unique_ptr<Character>> _characters;
};

The last three vectors are the vectors that I was trying to add to my header file. I have added them to another class in a similar way. And in that class, it did not lead to an error. But in my player header file, it does. Could you help me to solve/clarify this error?

All help will be appreciated.

Aucun commentaire:

Enregistrer un commentaire