我正在制作纸牌游戏,玩家在牌组中拥有不同类型的牌。我通过继承(和虚函数)实现了这个,以及一个简单的枚举类,它将帮助我跟踪每张卡的类型。所有这些都是背景信息,我不确定它有多相关,但我认为无论如何我都应该把它放在这里。
作为游戏的一部分(有不确定数量的玩家> = 2),我首先需要将玩家洗牌(其牌组作为唯一指针的向量传递给竞技场类的构造函数,其中为了随机化转弯顺序,负责运行游戏。当我在玩家的向量上调用std :: shuffle时,我得到有关unique_pointers删除交换的模板呕吐。然而,查找互联网告诉我,unique_ptrs可以被洗牌,而我的班级玩家只是一组对牌的unique_ptrs载体。
如果有帮助,我正在使用GCC 4.8.1(通过MinGW-builds)。
相关代码如下(所有相关标题都已包含在内,我非常确定这不是问题):
using namespace std;
random_device rd;
mt19937 gen(rd());
class card {
string code;
};
class zone {
protected:
vector<unique_ptr<card>> cards;
public:
void append(unique_ptr<card>& temp) {
cards.push_back(move(temp));
}
//Rest of stuff not relevant
};
class player {
public:
zone deck;
zone hand;
zone discard_pile;
zone prizes;
Field playarea;
player(int field_size, int max_repeat,vector<unique_ptr<card>>& cards,int DECK_SIZE, int prize_count) {
for (auto it=cards.begin();it!=cards.end();++it)
deck.append(*it);
deck.validate(max_repeat); //Implemented in Deck, doesn't have problems
}
};
class arena {
vector<player> battlers;
public:
arena(vector<vector<unique_ptr<card>>>& arr) {
for (int i=0;i<2;++i) {
battlers.emplace_back(6,4,arr[i],2,6);
}
shuffle(battlers.begin(),battlers.end(),gen);
}
};
//Providing some vectors which can be passed as dummies from main().
vector<unique_ptr<card>> temp,temp2;
unique_ptr<card> a1= new card;
unique_ptr<card> a2= new card;
unique_ptr<card> a3= new card;
unique_ptr<card> a4= new card;
temp.push_back(move(a1));
temp.push_back(move(a2));
temp2.push_back(move(a3));
temp2.push_back(move(a4));
vector<vector<unique_ptr<card>>> tempo;
tempo.push_back(move(temp));
tempo.push_back(move(temp2));
arena(tempo) arentemp;
我检查了shuffle的语法,看起来是正确的。有什么关于unique_ptr的东西,我在这里不见了吗?
(如果需要进一步的信息,请告诉我,我将在其中进行编辑。)
编辑: 我相信模板呕吐的部分是相关的:
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_construct.h|75|error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = card; _Dp = std::default_delete<card>]'|
The template vomit, as requested, in all its wonderful entirety:
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_construct.h||In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<card>; _Args = {const std::unique_ptr<card, std::default_delete<card> >&}]':|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_uninitialized.h|75|required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<card>*, std::vector<std::unique_ptr<card> > >; _ForwardIterator = std::unique_ptr<card>*; bool _TrivialValueTypes = false]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_uninitialized.h|117|required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<card>*, std::vector<std::unique_ptr<card> > >; _ForwardIterator = std::unique_ptr<card>*]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_uninitialized.h|258|required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<card>*, std::vector<std::unique_ptr<card> > >; _ForwardIterator = std::unique_ptr<card>*; _Tp = std::unique_ptr<card>]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_vector.h|316|required from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<card>; _Alloc = std::allocator<std::unique_ptr<card> >]'|
D:\NEO TCG FILES\Cards.hpp|143|required from 'void std::swap(_Tp&, _Tp&) [with _Tp = player]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|147|required from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<player*, std::vector<player> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<player*, std::vector<player> >]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algo.h|4410|required from 'void std::shuffle(_RAIter, _RAIter, _UGenerator&&) [with _RAIter = __gnu_cxx::__normal_iterator<player*, std::vector<player> >; _UGenerator = std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u>&]'|
D:\NEO TCG FILES\Cards.hpp|257|required from here|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_construct.h|75|error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = card; _Dp = std::default_delete<card>]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\unique_ptr.h|273|error: declared here|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h||In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const std::unique_ptr<card>*; _OI = std::unique_ptr<card>*]':|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|390|required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const std::unique_ptr<card>*; _OI = std::unique_ptr<card>*]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|428|required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const std::unique_ptr<card>*, std::vector<std::unique_ptr<card> > >; _OI = __gnu_cxx::__normal_iterator<std::unique_ptr<card>*, std::vector<std::unique_ptr<card> > >]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|460|required from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const std::unique_ptr<card>*, std::vector<std::unique_ptr<card> > >; _OI = __gnu_cxx::__normal_iterator<std::unique_ptr<card>*, std::vector<std::unique_ptr<card> > >]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\vector.tcc|199|required from 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<card>; _Alloc = std::allocator<std::unique_ptr<card> >]'|
D:\NEO TCG FILES\Cards.hpp|143|required from 'void std::swap(_Tp&, _Tp&) [with _Tp = player]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|147|required from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<player*, std::vector<player> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<player*, std::vector<player> >]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algo.h|4410|required from 'void std::shuffle(_RAIter, _RAIter, _UGenerator&&) [with _RAIter = __gnu_cxx::__normal_iterator<player*, std::vector<player> >; _UGenerator = std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u>&]'|
D:\NEO TCG FILES\Cards.hpp|257|required from here|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|335|error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = card; _Dp = std::default_delete<card>]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\unique_ptr.h|274|error: declared here|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h||In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = std::unique_ptr<card>*; _OI = std::unique_ptr<card>*]':|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|390|required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = std::unique_ptr<card>*; _OI = std::unique_ptr<card>*]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|428|required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = std::unique_ptr<card>*; _OI = std::unique_ptr<card>*]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|460|required from '_OI std::copy(_II, _II, _OI) [with _II = std::unique_ptr<card>*; _OI = std::unique_ptr<card>*]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\vector.tcc|205|required from 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<card>; _Alloc = std::allocator<std::unique_ptr<card> >]'|
D:\NEO TCG FILES\Cards.hpp|143|required from 'void std::swap(_Tp&, _Tp&) [with _Tp = player]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|147|required from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<player*, std::vector<player> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<player*, std::vector<player> >]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algo.h|4410|required from 'void std::shuffle(_RAIter, _RAIter, _UGenerator&&) [with _RAIter = __gnu_cxx::__normal_iterator<player*, std::vector<player> >; _UGenerator = std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u>&]'|
D:\NEO TCG FILES\Cards.hpp|257|required from here|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|335|error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = card; _Dp = std::default_delete<card>]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\unique_ptr.h|274|error: declared here|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_construct.h||In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<card>; _Args = {std::unique_ptr<card, std::default_delete<card> >&}]':|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_uninitialized.h|75|required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::unique_ptr<card>*; _ForwardIterator = std::unique_ptr<card>*; bool _TrivialValueTypes = false]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_uninitialized.h|117|required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::unique_ptr<card>*; _ForwardIterator = std::unique_ptr<card>*]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_uninitialized.h|258|required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::unique_ptr<card>*; _ForwardIterator = std::unique_ptr<card>*; _Tp = std::unique_ptr<card>]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\vector.tcc|209|required from 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<card>; _Alloc = std::allocator<std::unique_ptr<card> >]'|
D:\NEO TCG FILES\Cards.hpp|143|required from 'void std::swap(_Tp&, _Tp&) [with _Tp = player]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algobase.h|147|required from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<player*, std::vector<player> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<player*, std::vector<player> >]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_algo.h|4410|required from 'void std::shuffle(_RAIter, _RAIter, _UGenerator&&) [with _RAIter = __gnu_cxx::__normal_iterator<player*, std::vector<player> >; _UGenerator = std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u>&]'|
D:\NEO TCG FILES\Cards.hpp|257|required from here|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\stl_construct.h|75|error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = card; _Dp = std::default_delete<card>]'|
d:\c++11\mingw32\lib\gcc\i686-w64-mingw32\4.8.1\include\c++\bits\unique_ptr.h|273|error: declared here|
||=== Build failed: 8 error(s), 36 warning(s) (0 minute(s), 1 second(s)) ===|
答案 0 :(得分:1)
你遗失的unique_ptr
没什么,因为失败的是改变player
s的向量,而不是单向指针的向量。
或者:
vector<unique_ptr<player>> battlers;
或std::swap
专门化player
,因为默认实现由于某种原因不起作用。 之所以不起作用的原因未在问题中显示。我怀疑你应该在给予玩家之前将一系列独特的指针洗牌,但是我不知道你在做什么,所以我可以肯定地说。