Unresolved overload type in parameter to std::random_shuffle

J

Juha Nieminen

Any suggestions how to make this compile?

//-----------------------------------------------------------------------
#include <algorithm>
#include <cstdlib>

// The actual rng replaced with std::rand() for simplicity
unsigned randValue() { return std::rand(); }
unsigned randValue(unsigned modulo) { return std::rand() % modulo; }

int main()
{
int table[10];
std::random_shuffle(table, table+10, randValue);
}
//-----------------------------------------------------------------------

(Other than removing the first randValue() function, of course.)
 
S

SG

   Any suggestions how to make this compile?

// The actual rng replaced with std::rand() for simplicity
unsigned randValue() { return std::rand(); }
unsigned randValue(unsigned modulo) { return std::rand() % modulo; }
int main()
{
     int table[10];
     std::random_shuffle(table, table+10, randValue);
}
//-----------------------------------------------------------------------
   (Other than removing the first randValue() function, of course.)

        unsigned(*pf)(unsigned) = &randValue;
        std::random_shuffle(table, table+10, pf);

Alternatives:

(1) static_cast:

random_shuffle(...,static_cast<unsigned(*)(unsigned)>(randValue));

(2) A C++2011 lambda expression:

random_shuffle(...,[](unsigned i){return randValue(i);});

Cheers!
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top