【发布时间】:2014-06-16 18:49:27
【问题描述】:
我正在尝试创建一个包含均匀分布随机数的类。我使用 Visual Studio 2010 c++。
http://msdn.microsoft.com/en-us/library/vstudio/ee462299%28v=vs.100%29.aspx http://msdn.microsoft.com/en-us/library/ee462299.aspx
从这两个链接中尝试了许多组合,但我找不到满足我需求的解决方案。 我当前的代码是:
A) 我的问题.h
#include <random>
class MyProblem
{
public:
int calculateMyProblem();
private:
double _uniformRandNum;
std::mt19937 _generator(1729); // for 1729 I get ERROR: Expected a type specifier
std::uniform_real_distribution<> _distribution(0,1); // for both 0 an 1 i get ERROR: Expected a type specifier
void generateUniformDistrNum();
void calculateMeanDistance();
};
对于这两行我得到了错误
error C2059: syntax error : 'constant'
error C2059: syntax error : 'constant'
B) 我的问题.cpp
MyProblem::MyProblem()
{}
int MyProblem::calculateMyProblem()
{
generateUniformDistrNum();
// other stuff to be done with random number _uniformRandNum
}
void MyProblem::generateUniformDistrNum()
{
_uniformRandNum = (double) _distribution(_generator); // ERROR, see below
}
生成的错误是:
error C3867: 'MyProblem::_generator': function call missing argument list; use '&MyProblem::_generator' to create a pointer to member
error C2660: 'MyProblem::_distribution' : function does not take 1 arguments
我尝试了一整天,现在我无法弄清楚。我该如何解决这些问题?
【问题讨论】:
-
在 C++ 中,构造函数用于初始化对象及其成员。
标签: c++ visual-studio-2010 random