简体   繁体   中英

Mingw compilation error using

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM