vendredi 23 avril 2021

How to make class-wide use of

I am trying to use the C++ random library by initializing it in the ctor and using it in private member functions. I am able to make the simple code in the highest rated answer from Generating random integer from a range work in a main with no functions, but I cannot get it to compile in a basic class with a header. What is needed to make the random library work in this kind of structure?

Randy.h

#ifndef Randy_H_
#define Randy_H_

#include <random>

class Randy
{
    public:
        Randy();
        unsigned short int Doit();
    private:
        std::random_device rd;
        std::mt19937 rng();
        std::uniform_int_distribution<unsigned short int> InRange1();
        std::uniform_int_distribution<unsigned short int> InRange2();

        unsigned short int GenerateBar();
};
#endif

Randy.cpp

#include <random>
#include "Randy.h"

using namespace std;

Randy::Randy()
{
    std::random_device rd;
    std::mt19937 rng(rd());
    std::uniform_int_distribution<unsigned short int> InRange1(0, 180);
    std::uniform_int_distribution<unsigned short int> InRange2(150, 15000);
}

unsigned short int Randy::Doit()
{
    unsigned short int x = GenerateBar();
    return x;
}

unsigned short int Randy::GenerateBar()
{
    unsigned short int oof = InRange1(rng);
    unsigned short int rab = InRange2(rng);
    unsigned short int bar = oof + rab;
    return bar;
}

g++ on Windows 10 complains as follows:

Randy.cpp: In member function 'short unsigned int Randy::GenerateBar()':
Randy.cpp:22:42: error: no matching function for call to 'Randy::InRange1(<unresolved overloaded function type>)'
     unsigned short int oof = InRange1(rng);
                                          ^
In file included from Randy.cpp:2:
Randy.h:14:59: note: candidate: 'std::uniform_int_distribution<short unsigned int> Randy::InRange1()'
         std::uniform_int_distribution<unsigned short int> InRange1();
                                                           ^~~~~~~~
Randy.h:14:59: note:   candidate expects 0 arguments, 1 provided
Randy.cpp:23:42: error: no matching function for call to 'Randy::InRange2(<unresolved overloaded function type>)'
     unsigned short int rab = InRange2(rng);
                                          ^
In file included from Randy.cpp:2:
Randy.h:15:59: note: candidate: 'std::uniform_int_distribution<short unsigned int> Randy::InRange2()'
         std::uniform_int_distribution<unsigned short int> InRange2();
                                                           ^~~~~~~~
Randy.h:15:59: note:   candidate expects 0 arguments, 1 provided

Thanks in advance...




Aucun commentaire:

Enregistrer un commentaire