Jan 13, 2022 at 12:27pm UTC
I would like to declare a probability distribution within a class. Why does the desired form (2) in the below code fail, but any of positions (1), (3) or (4) work?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
mt19937 gen( time( 0 ) );
// uniform_real_distribution<double> uni( 0.0, 1.0 ); // (1) (OK)
class X
{
uniform_real_distribution<double > uni( 0.0, 1.0 ); // (2) (fails)
// uniform_real_distribution<double> uni = uniform_real_distribution<double>( 0.0, 1.0 ); // (3) (OK)
public :
void f()
{
// uniform_real_distribution<double> uni( 0.0, 1.0 ); // (4) (OK)
cout << uni( gen );
}
};
int main()
{
X x;
x.f();
}
Last edited on Jan 13, 2022 at 12:27pm UTC
Jan 13, 2022 at 12:44pm UTC
It's the difference between parentheses and curly braces. The latter works.