Domanda

Trying to get the example at the bottom of this cppreference page to run. I have modified it so that it doesn't use std::random_device which is not available on mingw. The exact code is below:

#include <iostream>
#include <random>

int main()
{
    std::mt19937 gen();
    std::uniform_real_distribution<> dis(1, 2);

    for (int n = 0; n < 10; ++n) {
        std::cout << dis(gen) << ' ';
    }
    std::cout << '\n';

    return 0;
}

Compile errors can be found at http://pastebin.com/hekFJ44G (expires in 1 month). Anyone know a work around? I tried combing through the source but I can't understand anything.

È stato utile?

Soluzione

When you modified the example to remove std::random_device, you changed:

std::mt19937 gen(rd());

which declares a object of type std::mt19937 named gen to:

std::mt19937 gen();

which is an instance of the dreaded "most vexing parse", so gen is a function declaration instead of a declaration of an mt19937 object.

Change that line of code to: std::mt19937 gen;

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top