1

我正在尝试基于http://www.sitmo.com/article/generating-random-numbers-in-c-with-boost/为 boost 随机数生成器创建一个包装类。问题是 boost 使用模板,我不知道如何将声明GEN gen与实例化分开,就像DIST distdist = DIST(0, 1). 任何建议表示赞赏。

fr.hpp:

#include <boost/random/variate_generator.hpp>
#include <boost/generator_iterator.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>

class FR {

   private:

     typedef boost::mt19937                     ENG;    // Mersenne Twister
     typedef boost::normal_distribution<double> DIST;   // Normal Distribution
     typedef boost::variate_generator<ENG,DIST> GEN;    // Variate generator

     ENG eng;
     DIST dist;
     GEN gen;

   public:
      FR();
};

f.c:

#include "fr.hpp"

FR::FR() {
  dist = DIST(0, 1);
  gen = GEN(eng, dist);
}

不编译:

$ g++ -O3 -ggdb3 -Wall -c fr.cpp

fr.cpp: In constructor ‘FR::FR()’:
fr.cpp:3: error: no matching function for call to ‘boost::random::variate_generator<boost::random::mersenne_twister_engine<unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul, 1812433253u>, boost::random::normal_distribution<double> >::variate_generator()’

ETC

4

1 回答 1

1

boost::variate_generator没有默认构造函数,因此您需要使用构造函数的初始化列表

FR::FR()
: dist(0,1), gen(eng,dist)
{}
于 2013-07-17T05:09:05.407 回答