test1.cpp:9:77:错误:对"(const std::normal_distribution) <double>(std::mt19937&)"的调用不匹配

test1.cpp:9:77: error: no match for call to ‘(const std::normal_distribution<double>) (std::mt19937&)’

本文关键字:std lt double gt 不匹配 调用 mt19937 normal cpp 错误 test1      更新时间:2023-10-16

我有这段代码:

class Y {
private:
std::normal_distribution<double> N;
public:
Y() : N(0,1) {}
double operator()(const double & x, std::mt19937 G) const { return x + N(G); }
};

我有这个错误:

错误:对"(常量标准::normal_distribution(的调用不匹配 (标准::mt19937&('

对于行:

double operator()(const double & x, std::mt19937 G) const { return x + N(G); }

运算符std::normal_distribution<T>::operator()(Generator& g)是一个非常量成员函数,因此不能为常量对象调用。只需从函数定义中删除const

double operator()(const double & x, std::mt19937 &G) { return x + N(G); }

另请注意,您很可能希望通过引用传递生成器参数