Having toruble initializing a default_random_engine

I get a
1
2
  
no match for call to '(std::shared_ptr<std::linear_congruential_engine<unsigned int, 16807u, 0u, 2147483647u> >) (std::default_random_engine*)'


on the line (in main()) highlighted by CDT:
 
Options::prng(new default_random_engine(Options::SEED));


I have included my Options.hpp file which contains
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef OPTIONS_HPP_
#define OPTIONS_HPP_

#include <random>
#include <memory>

using namespace std;

class Options {
public:
	 static const long SEED = 42;
	 static shared_ptr<default_random_engine> prng;
//other stuff
};

#endif /* OPTIONS_HPP_ */ 


How can I resolve this issue?
Last edited on
closed account (SECMoG1T)
Hi, i guess you need to initialize your static variables outside that class because they will be created before any class instance is constructed, you might do this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef OPTIONS_HPP_
#define OPTIONS_HPP_

class Options {
public:
	 static const long SEED;
	 static shared_ptr<default_random_engine> prng;
//other stuff
};

const long Options::SEED =42;
shared_ptr<default_random_engine> Options::prng(make_shared<default_random_engine>(Options::SEED));

#endif /* OPTIONS_HPP_ */ 
Last edited on
phi1 wrote:
on the line (in main())
No, you cannot do this in main, or in any function for that matter. It mus be done in the same (global) scope as the class.

Generally, this is a bad idea - I would recommend that the random number generator be passed to the instance by reference so that the calling code can do as it wishes with regards to the RNG.
Last edited on
Thanks for your suggestion. I tried it but unfortunately it's still showing the same error. Do you know what else could cause the issue?

EDIT: My bad: I i forgot to remove the line from main()

EDIT2: thanks for the best practice LB, but I'm too lazy to change all my code atm, especially since I do not plan to modify the generator.
Last edited on
Not sure why andy was suggesting using a shared_ptr, that sounds like a bad idea.
I was using shared_ptr from the start. Why is it a bad idea?
closed account (SECMoG1T)
@LB Not sure why andy was suggesting using a shared_ptr I wasn't suggesting that the Op use shared_ptr , i just couldn't figure out why in the first place he used them in an rng given the short snippet, so i just decided to correct his error n that's all.
Oops, sorry I misread.

Yes @phi1 - it's a bad idea to use shared pointer for no reason. andy shows how to initialize it.
Topic archived. No new replies allowed.