I have a std::vector<std::string> tileSet_
as a member variable of a class. In the constructor of said class I call the function
void MapWindow::shuffleTiles() const
{
std::vector<std::pair<std::string, unsigned>> tileAmounts = {
{"a", 2}, {"b", 4}, {"c", 1}, {"d", 4},
{"e", 5}, {"f", 2}, {"g", 1}, {"h", 3},
{"i", 2}, {"j", 3}, {"k", 3}, {"l", 3},
{"m", 2}, {"n", 3}, {"o", 2}, {"p", 3},
{"q", 1}, {"r", 3}, {"s", 2}, {"t", 1},
{"u", 8}, {"v", 9}, {"w", 4}, {"x", 1}
};
int tileN = 0;
for (std::vector<std::pair<std::string, int>>::size_type i = 0;
i < tileAmounts.size();i++) {
tileN += tileAmounts.at(i).second;
}
// only used once to initialise (seed) engine
std::random_device rd;
// random-number engine used (Mersenne-Twister in this case)
std::mt19937 rng(rd());
// guaranteed unbiased
std::uniform_int_distribution<int> uni(0,tileAmounts.size() - 1);
for (int i = 0; i < tileN; i++) {
auto random_integer = uni(rng);
//qDebug() << i << ":" << random_integer;
std::cout << i
<< ":"
<< tileAmounts.at(random_integer).first
<< ":"
<< tileAmounts.at(random_integer).second
<< std::endl
<< std::endl;
while (tileAmounts.at(random_integer).second < 1) {
std::cout << "Reshuffling "
<< tileAmounts.at(random_integer).first
<< ":"
<< tileAmounts.at(random_integer).second
<< std::endl;
random_integer = uni(rng);
}
std::string s = tileAmounts[random_integer].first;
std::cout << s
<< std::endl;
tileSet_.push_back(s);
tileAmounts.at(random_integer).second -= 1;
}
for (std::vector<std::string>::size_type i = 0;
i < tileSet_.size();i++) {
std::cout << tileSet_.at(i)
<< std::endl;
}
}
which at the very end attempts to push back a randomly picked string from a pair contained in the vector tileAmounts
into the memeber vector. For some reason, this raises the error
no matching member function call to 'push_back'
Why does this happen. I'm taking the vector to be pushed back from a std::pair
, but surely that shouldn't be causing any type mismatches? A std::string
in a std::pair
contained in a std::vector
is still a std::string
, no?