lundi 4 février 2019

How to declare a class template in a structure and initialize later?

I am trying to model a tolerance stackup. I made a structure Layer that holds the lower limit (tol[0]) and upper (tol[1]) limit of the tolerance range. I want to generate a random value between tol[0] and tol[1] and assign it to val.

My implementation declares the uniform_real_distribution class template in the structure and initializes it in main(), but I'm getting errors during compilation that make me think I can't use class templates this way.

#include <random>

struct Layer {
    double tol[2];
    double val;
    std::string name;
    std::uniform_real_distribution<double> distribution;
};

int main() 
{
    Layer block;
    block.tol[0] = .240;
    block.tol[1] = .260;

    std::default_random_engine generator;
    block.distribution(block.tol[0],block.tol[1]);
    block.val = block.distribution(generator);

    return 0;
}

I'm getting the following error from g++:

error: no match for call to '(std::uniform_real_distribution<double>) (double&, double&)'
    block.distribution(block.tol[0],block.tol1[]);
                                                ^

I've got quite a few Layer structures that I create so I was hoping to associate the distribution with struct, but I'm not sure that it is possible anymore.




Aucun commentaire:

Enregistrer un commentaire