为什么在向 header 文件添加具有唯一指针的向量后出现 "Attempting to reference a deleted function" 错误?
Why did I get the: "Attempting to reference a deleted function" error after adding a vector with unique pointers to my header file?
为了学习 C++,我正在编写一个游戏。在这个游戏中,可以有两个玩家。这些播放器能够通过插座相互连接。添加带有指向播放器 header 的唯一指针的向量后,我收到错误:"Error C2280 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function"。建筑矢量也有同样的错误。
为了更清楚地了解问题所在,我在下面的代码块中设置了我的 header 文件:
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;
};
最后三个矢量是我试图添加到我的 header 文件中的矢量。我以类似的方式将它们添加到另一个 class。在那 class 中,它没有导致错误。但在我的播放器 header 文件中,确实如此。你能帮我解决 solve/clarify 这个错误吗?
我们将不胜感激。
编辑:试图解释为什么这个问题不是重复的。
它可能是重复的,但就目前而言,据我所知,问题是:“Why can I not push_back a unique_ptr into a vector?”有人试图调用 push_back 函数而不使用 std::move 在 push_back 中。目前我并没有尝试将值放入我的向量中。我想要的是我的 header 中的唯一指针向量。所以我还没有尝试用元素填充向量。因此,我倾向于认为还不可能将 std::move 用于建筑物和字符向量。我知道在不调用 std::move 函数时经常会发生此错误,因为我查看了其他 Whosebug 帖子。但是 none 似乎对我的问题有正确的答案。因为大多数人的答案是:使用 "std::move" 或使用 "default constructor" 而这并没有解决我的问题。
我的预期是,它与被定义为 shared_ptr 的播放器有关,而播放器是向量的容器,这可能是问题的一部分吗?
很可能,您正在将 Player 对象复制到某个地方,默认情况下是每个字段的元素复制。您不能复制唯一指针的向量
如果 class 中没有定义复制构造函数,编译器将提供一个默认复制构造函数(以及赋值运算符)。此默认复制构造函数将尝试复制 class 成员,在这种情况下包括唯一指针的向量。
必须使 class 明确不可复制:
Player(const Player&) = delete;
Player& operator =(const Player&) = delete;
为了学习 C++,我正在编写一个游戏。在这个游戏中,可以有两个玩家。这些播放器能够通过插座相互连接。添加带有指向播放器 header 的唯一指针的向量后,我收到错误:"Error C2280 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function"。建筑矢量也有同样的错误。
为了更清楚地了解问题所在,我在下面的代码块中设置了我的 header 文件:
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;
};
最后三个矢量是我试图添加到我的 header 文件中的矢量。我以类似的方式将它们添加到另一个 class。在那 class 中,它没有导致错误。但在我的播放器 header 文件中,确实如此。你能帮我解决 solve/clarify 这个错误吗?
我们将不胜感激。
编辑:试图解释为什么这个问题不是重复的。
它可能是重复的,但就目前而言,据我所知,问题是:“Why can I not push_back a unique_ptr into a vector?”有人试图调用 push_back 函数而不使用 std::move 在 push_back 中。目前我并没有尝试将值放入我的向量中。我想要的是我的 header 中的唯一指针向量。所以我还没有尝试用元素填充向量。因此,我倾向于认为还不可能将 std::move 用于建筑物和字符向量。我知道在不调用 std::move 函数时经常会发生此错误,因为我查看了其他 Whosebug 帖子。但是 none 似乎对我的问题有正确的答案。因为大多数人的答案是:使用 "std::move" 或使用 "default constructor" 而这并没有解决我的问题。
我的预期是,它与被定义为 shared_ptr 的播放器有关,而播放器是向量的容器,这可能是问题的一部分吗?
很可能,您正在将 Player 对象复制到某个地方,默认情况下是每个字段的元素复制。您不能复制唯一指针的向量
如果 class 中没有定义复制构造函数,编译器将提供一个默认复制构造函数(以及赋值运算符)。此默认复制构造函数将尝试复制 class 成员,在这种情况下包括唯一指针的向量。
必须使 class 明确不可复制:
Player(const Player&) = delete;
Player& operator =(const Player&) = delete;