#include <array>
#include <random>
#define MAX_TILES 100
class TILE; // Forward (polymorphic)
class BAG
{
public:
typedef std::array<TILE *, MAX_TILES> bag_t;
private:
int m_count = 0;
bag_t ma_bag;
public:
void Shuffle();
};
void BAG::Shuffle()
{
bag_t::const_iterator first = &ma_bag[0];
bag_t::const_iterator last = &ma_bag[m_count];
std::shuffle(first, last, std::default_random_engine());
}
I'm having a problem with iterators and std:array.
BAG is a container class that contains a std:array and a count.
I can't seem to find the correct expression for first and last.
The errors I get are:
c:\dev\junk\junk.cpp(23): error C2440: 'initializing': cannot convert from 'TILE **' to 'std::_Array_const_iterator<_Ty,100>'
c:\dev\junk\junk.cpp(23): note: Constructor for class 'std::_Array_const_iterator<_Ty,100>' is declared 'explicit'
c:\dev\junk\junk.cpp(24): error C2440: 'initializing': cannot convert from 'TILE **' to 'std::_Array_const_iterator<_Ty,100>'
c:\dev\junk\junk.cpp(24): note: Constructor for class 'std::_Array_const_iterator<_Ty,100>' is declared 'explicit'
I understand how the compiler arrived at TILE **, but I don't understand what it's looking for on the left side.
I've tried various permutations: ma_bag[0], &(ma_bag[0]), etc.
ma_bag.begin() does work, but I can't use ma_bag.end() as that assumes ma_bag[100], rather than using ma_count for the number of valid elements.
Yes, I know a std::vector would eliminate the need to maintain my own count and I may resort to that.
Possibly - depends upon what is wanted. If you only want to shuffle the first m_count items - then yes. If you want to shuffle the whole container - then no.