提升随机数 - 奇怪的编译错误

发布于 2024-10-21 03:28:47 字数 2083 浏览 4 评论 0原文

在我正在编写的模拟中,我有一个类代表必须采取某些操作的代理,并且我希望该代理能够访问随机数生成器。我听说 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衬遮猫 2024-10-28 03:28:47

我认为您需要在构造函数初始化列表中初始化成员变量 uniuniRng ,而不是在声明它们的地方内联。

I think you need to initialise you member variables uni and uniRng in the constructor initialisation list rather than inline where they are declared.

祁梦 2024-10-28 03:28:47

这两行:

boost::uniform_real<> uni(0.0,1.0);
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> >   uniRng(rng, uni);

应该是

boost::uniform_real<> uni;
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> > uniRng;

,并且这两个变量必须在构造函数中初始化,如下所示

Agent() 
    : uni(0., 1.), uniRng(rng, uni) 
{
    rng.seed(static_cast<boost::uint32_t> (std::time(0)));
}

These two lines:

boost::uniform_real<> uni(0.0,1.0);
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> >   uniRng(rng, uni);

should be

boost::uniform_real<> uni;
boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_real<> > uniRng;

and those two variables have to be initialized in the constructor, as

Agent() 
    : uni(0., 1.), uniRng(rng, uni) 
{
    rng.seed(static_cast<boost::uint32_t> (std::time(0)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文