The class "montecarlo" contains lambda as a member variable. This code can be compiled, but will cause "Segmentation fault(core dumped)" in run time. Could you explaine how to fix it?
#include<random>
#include<functional>
#include<iostream>
class montecarlo
{
public:
montecarlo(double x_min, double x_max);
std::function<double()> rand;
};
montecarlo::montecarlo(double x_min, double x_max){
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> rand_(x_min, x_max);
rand = [&](){return rand_(mt);};
}
int main(){
montecarlo x(0, 1);
std::cout<<x.rand()<<std::endl;
}
And what made me wonder is when I change the constructor's implementation into the code below, it worked:
montecarlo::montecarlo(double x_min, double x_max){
rand = [](){return 0;};
}
You would probably know, but let me say that what I want to do is not just using a random functions. Thanks.
Aucun commentaire:
Enregistrer un commentaire