c++11 - C++ Shuffle Vector -
i have seen other threads on none seem answer question. i'm trying use shuffle()
function on vector keep getting error:
deck_of_cards.cpp:34:5: error: no matching function call 'shuffle' shuffle (deck.begin(), deck.end(), std::default_random_engine(seed)); ^~~~~~~ /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:3049:10: note: candidate function [with _randomaccessiterator = std::__1::__wrap_iter<card *>, _uniformrandomnumbergenerator = std::__1::linear_congruential_engine<unsigned int, 48271, 0, 2147483647>] not viable: expects l-value 3rd argument void shuffle(_randomaccessiterator __first, _randomaccessiterator __last, ^ 1 error generated.
i'm using clang++ compiler , code throwing error:
void deckofcards::shuffledeck() { seed = std::chrono::system_clock::now().time_since_epoch().count(); shuffle (deck.begin(), deck.end(), std::default_random_engine(seed)); }
i have correct #includes
in program. i'm new c++ , java program have i'm trying convert c++. appreciated!
the compiler telling expects lvalue ar third parameter, , giving rvalue (in form of temporary object).
try this:
auto gen = std::default_random_engine(seed); shuffle (deck.begin(), deck.end(), gen);
note std::shuffle
should accept rvalue third parameter, standards-compliant implementation have accepted code.
Comments
Post a Comment