提升随机数 - 奇怪的编译错误
在我正在编写的模拟中,我有一个类代表必须采取某些操作的代理,并且我希望该代理能够访问随机数生成器。我听说 boost rng 非常好,所以我想学习如何使用它们。
所以,问题来了。这段代码编译并运行完美:
//Random.cpp
#include <boost/random.hpp>
#include <boost/limits.hpp>
#include <iostream>
#include <ctime>
int main()
{
int N = 10;
boost::lagged_fibonacci607 rng;
rng.seed(static_cast<boost::uint32_t> (std::time(0)));
boost::uniform_real<> uni(0.0,1.0);
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> > uniRng(rng, uni);
for (int i = 0; i < N; ++i)
std::cout << uniRng() << std::endl;
return 0;
}
所以,我希望我的代理类能够访问类型为:的私有对象,
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> >
当代理需要随机数时可以调用该对象。
所以我尝试做的是:
//RandomAgent.cpp
#include <boost/random.hpp>
#include <boost/limits.hpp>
#include <iostream>
#include <ctime>
class Agent {
private:
boost::lagged_fibonacci607 rng;
boost::uniform_real<> uni(0.0,1.0);
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> > uniRng(rng, uni);
public:
Agent() {
rng.seed(static_cast<boost::uint32_t> (std::time(0)));
}
void show() {
std::cout << uniRng() << std::endl;
}
};
int main() {
Agent foo;
for(int i = 0; i < 10; ++i)
foo.show()
}
并且我收到以下错误消息:
$ g++ RandomAgent.cpp
Random.cpp:10: error: expected identifier before numeric constant
Random.cpp:10: error: expected ‘,’ or ‘...’ before numeric constant
Random.cpp:11: error: ‘rng’ is not a type
Random.cpp:11: error: ‘uni’ is not a type
Random.cpp: In member function ‘void Agent::show()’:
Random.cpp:18: error: no matching function for call to ‘Agent::uniRng()’
Random.cpp:11: note: candidates are: boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real
>代理::uniRng(int, int)
In a simulation I'm writing I have a class that represents an Agent that must take some actions and I want this agent to have access to a random number generator. I heard boost rng's where good ones, so I wanted to learn how to use them.
So, here's the problem. This code compiles and runs perfectly:
//Random.cpp
#include <boost/random.hpp>
#include <boost/limits.hpp>
#include <iostream>
#include <ctime>
int main()
{
int N = 10;
boost::lagged_fibonacci607 rng;
rng.seed(static_cast<boost::uint32_t> (std::time(0)));
boost::uniform_real<> uni(0.0,1.0);
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> > uniRng(rng, uni);
for (int i = 0; i < N; ++i)
std::cout << uniRng() << std::endl;
return 0;
}
So, I wanted my Agent class to have access to have a private object of type:
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> >
that can be called when the agent needs a random number.
So what I tried to do is:
//RandomAgent.cpp
#include <boost/random.hpp>
#include <boost/limits.hpp>
#include <iostream>
#include <ctime>
class Agent {
private:
boost::lagged_fibonacci607 rng;
boost::uniform_real<> uni(0.0,1.0);
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> > uniRng(rng, uni);
public:
Agent() {
rng.seed(static_cast<boost::uint32_t> (std::time(0)));
}
void show() {
std::cout << uniRng() << std::endl;
}
};
int main() {
Agent foo;
for(int i = 0; i < 10; ++i)
foo.show()
}
And I got the following error messages:
$ g++ RandomAgent.cpp
Random.cpp:10: error: expected identifier before numeric constant
Random.cpp:10: error: expected ‘,’ or ‘...’ before numeric constant
Random.cpp:11: error: ‘rng’ is not a type
Random.cpp:11: error: ‘uni’ is not a type
Random.cpp: In member function ‘void Agent::show()’:
Random.cpp:18: error: no matching function for call to ‘Agent::uniRng()’
Random.cpp:11: note: candidates are: boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real
> Agent::uniRng(int, int)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要在构造函数初始化列表中初始化成员变量
uni
和uniRng
,而不是在声明它们的地方内联。I think you need to initialise you member variables
uni
anduniRng
in the constructor initialisation list rather than inline where they are declared.这两行:
应该是
,并且这两个变量必须在构造函数中初始化,如下所示
These two lines:
should be
and those two variables have to be initialized in the constructor, as