Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Unresolved overload type in parameter to std::random_shuffle (http://www.velocityreviews.com/forums/t806447-unresolved-overload-type-in-parameter-to-std-random_shuffle.html)

Juha Nieminen 11-30-2011 03:37 PM

Unresolved overload type in parameter to std::random_shuffle
 
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.)


SG 12-02-2011 06:57 PM

Re: Unresolved overload type in parameter to std::random_shuffle
 
On 30 Nov., 17:16, Leigh Johnston wrote:
> On 30/11/2011 15:37, Juha Nieminen wrote:
> > * *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.)

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


Alternatives:

(1) static_cast:

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

(2) A C++2011 lambda expression:

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

Cheers!
SG


All times are GMT. The time now is 09:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.