How can I use random library in a class? Can any one with kindness help me?

How can I use random library in a class? Can any one with kindness help me? I am using boost(version boost_1_37_0) random library in Dev-C++ with gcc. and the code is: #include "boost/random/normal_distribution.hpp" #include <boost/random.hpp> ................................. class TransferedSignal{ public: TransferedSignal(){ one(0.0,1.0) ; die(rng, one); }; double output(){ return die(); }; private: boost::mt19937 rng; boost::uniform_real<> one; boost::variate_generator<boost::mt19937&, boost::uniform_real<> > die; }; int main(int argc, char *argv[]) { TransferedSignal transferedsignal(); for (int i=0; i<100;i++) transferedsignal.output(); system("PAUSE"); return 0; } But the complier told me that: mainprojectme13.cpp: In constructor 'TransferedSignal::TransferedSignal()': mainprojectme13.cpp:154: error: no matching function for call to 'boost::variate_generator<boost::mt19937&, boost::uniform_real<double> >::variate_generator()' F:/CB2009/boost_1_37_0/boost/random/variate_generator.hpp:97: note: candidates are: boost::variate_generator<Engine, Distribution>::variate_generator(Engine, Distribution) [with Engine = boost::mt19937&, Distribution = boost::uniform_real<double>] F:/CB2009/boost_1_37_0/boost/random/variate_generator.hpp:87: note: boost::variate_generator<boost::mt19937&, boost::uniform_real<double> >::variate_generator(const boost::variate_generator<boost::mt19937&, boost::uniform_real<double> >&) mainprojectme13.cpp:155: error: no match for call to '(boost::uniform_real<double>) (double, double)' mainprojectme13.cpp:156: error: no match for call to '(boost::variate_generator<boost::mt19937&, boost::uniform_real<double> >) (boost::mt19937&, boost::uniform_real<double>&)' F:/CB2009/boost_1_37_0/boost/random/variate_generator.hpp:100: note: candidates are: typename Distribution::result_type boost::variate_generator<Engine, Distribution>::operator()() [with Engine = boost::mt19937&, Distribution = boost::uniform_real<double>] mainprojectme13.cpp: In function 'int main(int, char**)': mainprojectme13.cpp:173: error: request for member 'output' in 'transferedsignal', which is of non-class type 'TransferedSignal()' And I do not know how to solve it. Shauld I define "boost::variate_generator<Engine, Distribution>::variate_generator() " in the random library? But God knows what will be happened after that. Can any one with kindness help me? Thanks a lot.

2010/5/11 fmingu <fmingu@163.com>
................................. class TransferedSignal{ public: TransferedSignal(){ one(0.0,1.0) ; die(rng, one); };
Try replacing your constructor with this: TransferedSignal() : one(0.0,1.0), die(rng, one) {} Roman Perepelitsa.

在2010-05-11 18:05:00,"Roman Perepelitsa" <roman.perepelitsa@gmail.com> 写道: 2010/5/11 fmingu <fmingu@163.com> ................................. class TransferedSignal{ public: TransferedSignal(){ one(0.0,1.0) ; die(rng, one); }; Try replacing your constructor with this: TransferedSignal() : one(0.0,1.0), die(rng, one) {} Roman Perepelitsa. Oh,It works now. But can you tell me the reason about it? Is that a kind of C++ standard to do so? And when I wrote another programs like: #include <boost/numeric/mtl/mtl.hpp> using namespace mtl; using namespace mtl::matrix; typedef dense2D<double> Matrix; typedef dense_vector<double> Vec; class Used{ public: Used(){}; Used(Matrix inputmatrix, Vec inputvec):matrix(inputmatrix),vec(inputvec),value(111){}; Matrix getmatrix(){ return matrix;}; Vec getvec(){return vec;}; int getvalue(){return value;}; protected: Matrix matrix; Vec vec; int value; }; class Usedchild:public Used{ public: Usedchild(Matrix inputmatrix, Vec inputvec):matrix(inputmatrix),vec(inputvec){}; } the gcc complier told me that: mainprojectme9.cpp: In constructor 'Usedchild::Usedchild(Matrix, Vec)': mainprojectme9.cpp:92: error: class 'Usedchild' does not have any field named 'matrix' mainprojectme9.cpp:92: error: class 'Usedchild' does not have any field named 'vec' Can anyone tell me why? Thanks a lot.
participants (2)
-
fmingu
-
Roman Perepelitsa