gpt4 book ai didi

c++ - test1.cpp:9:77:错误:与 ‘(const std::normal_distribution) (std::mt19937&)’的调用不匹配

转载 作者:行者123 更新时间:2023-12-02 10:02:39 25 4
gpt4 key购买 nike

我有这个代码:

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); }
};

我有这个错误:

error: no match for call to ‘(const std::normal_distribution) (std::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); }

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

关于c++ - test1.cpp:9:77:错误:与 ‘(const std::normal_distribution<double>) (std::mt19937&)’的调用不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61834223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com