Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


Edit: An attempt to explain why this question is not a duplicate.

It might be a duplicate but for now, as far as I can see, is that in the question: "Why can I not push_back a unique_ptr into a vector?" someone is trying to call the push_back function without using the std::move in the push_back. At the moment I am not trying to put values inside my vector. What I want is a vector of unique pointers inside my header. So I am not trying to fill the vectors with elements yet. Therefore I tend to think that it is not possible to make use of std::move for the building and character vectors yet. I know this error often occurs when not calling the std::move function because I have looked at the other StackOverflow posts. But none of them seems to have the right answer to my question. Because the answer to most of them is: use "std::move" or use a "default constructor" and that did not solve my problem.

My expectation is that it has something to do with player being defined as a shared_ptr and player is the container for the vectors could that be a part of the problem?

like image 506
user3473161 Avatar asked Jan 22 '17 18:01

user3473161


2 Answers

If there is no copy constructor defined in a class, a compiler will provide a default one (along with the assignment operator). This default copy constructor will attempt to copy class members, including a vector of unique pointers in this case.

One has to make the class explicitly non copyable:

Player(const Player&) = delete;
Player& operator =(const Player&) = delete;
like image 195
Archie Avatar answered Nov 20 '22 08:11

Archie


Most probably, you are copying Player object somewhere which is by default element copying of every field. You can't copy a vector of unique pointers

like image 3
RiaD Avatar answered Nov 20 '22 07:11

RiaD