gpt4 book ai didi

c++ - uniform_real_distribution operator() 编译错误

转载 作者:太空狗 更新时间:2023-10-29 20:33:26 25 4
gpt4 key购买 nike

我写了这段代码:

std::pair<std::weak_ptr<Node>, size_t> NodeSelector::rouletteWheel() const {
const std::uniform_real_distribution<long double> urd;
size_t i = 0;
for(auto rnd = urd(shared_random_engine); rnd >= 0; ++i) {
rnd -= getNormValue(i);
}
return std::make_pair(entries_[i - 1], i - 1);
}

其中“shared_random_engine”以这种方式初始化(​​在单独的.h 文件中)

std::minstd_rand shared_random_engine(static_cast<unsigned int>(42));

只要我在 Windows 上编译它,一切正常,但是当我尝试在 Ubuntu 18.04.2 上使用 g++ 7.3.0 编译时,我得到这个错误:

montecarlo.cpp: In member function ‘std::pair<std::weak_ptr<MonteCarloNode>, long unsigned int> NodeSelector::rouletteWheel() const’:
montecarlo.cpp:728:41: error: no match for call to ‘(const std::uniform_real_distribution<long double>) (std::minstd_rand&)’
for(auto rnd = urd(shared_random_engine); rnd >= 0; ++i) {
^
In file included from /usr/include/c++/7/random:49:0,
from selector.h:15,
from montecarlo.h:13,
from montecarlo.cpp:7:
/usr/include/c++/7/bits/random.h:1813:2: note: candidate: std::uniform_real_distribution<_RealType>::result_type std::uniform_real_distribution<_RealType>::operator()(_UniformRandomNumberGenerator&) [with _UniformRandomNumberGenerator = std::linear_congruential_engine<long unsigned int, 48271, 0, 2147483647>; _RealType = long double; std::uniform_real_distribution<_RealType>::result_type = long double] <near match>
operator()(_UniformRandomNumberGenerator& __urng)
^~~~~~~~
/usr/include/c++/7/bits/random.h:1813:2: note: passing ‘const std::uniform_real_distribution<long double>*’ as ‘this’ argument discards qualifiers
/usr/include/c++/7/bits/random.h:1818:2: note: candidate: template<class _UniformRandomNumberGenerator> std::uniform_real_distribution<_RealType>::result_type std::uniform_real_distribution<_RealType>::operator()(_UniformRandomNumberGenerator&, const std::uniform_real_distribution<_RealType>::param_type&) [with _UniformRandomNumberGenerator = _UniformRandomNumberGenerator; _RealType = long double]
operator()(_UniformRandomNumberGenerator& __urng,
^~~~~~~~
/usr/include/c++/7/bits/random.h:1818:2: note: template argument deduction/substitution failed:
montecarlo.cpp:728:41: note: candidate expects 2 arguments, 1 provided
for(auto rnd = urd(shared_random_engine); rnd >= 0; ++i) {
^

由于错误的最后几行告诉我需要 2 个参数,所以我按以下方式更改了代码:

for(auto rnd = urd(shared_random_engine, urd.param())

在 Windows 上,它仍然工作正常,但在 Ubuntu 上我得到这个错误,现在告诉我只传递 1 个参数:

montecarlo.cpp: In member function ‘std::pair<std::weak_ptr<MonteCarloNode>, long unsigned int> NodeSelector::rouletteWheel() const’:
montecarlo.cpp:728:54: error: no match for call to ‘(const std::uniform_real_distribution<long double>) (std::minstd_rand&, std::uniform_real_distribution<long double>::param_type)’
for(auto rnd = urd(shared_random_engine, urd.param()); rnd >= 0; ++i) {
^
In file included from /usr/include/c++/7/random:49:0,
from selector.h:15,
from montecarlo.h:13,
from montecarlo.cpp:7:
/usr/include/c++/7/bits/random.h:1813:2: note: candidate: template<class _UniformRandomNumberGenerator> std::uniform_real_distribution<_RealType>::result_type std::uniform_real_distribution<_RealType>::operator()(_UniformRandomNumberGenerator&) [with _UniformRandomNumberGenerator = _UniformRandomNumberGenerator; _RealType = long double]
operator()(_UniformRandomNumberGenerator& __urng)
^~~~~~~~
/usr/include/c++/7/bits/random.h:1813:2: note: template argument deduction/substitution failed:
montecarlo.cpp:728:54: note: candidate expects 1 argument, 2 provided
for(auto rnd = urd(shared_random_engine, urd.param()); rnd >= 0; ++i) {
^
In file included from /usr/include/c++/7/random:49:0,
from selector.h:15,
from montecarlo.h:13,
from montecarlo.cpp:7:
/usr/include/c++/7/bits/random.h:1818:2: note: candidate: std::uniform_real_distribution<_RealType>::result_type std::uniform_real_distribution<_RealType>::operator()(_UniformRandomNumberGenerator&, const std::uniform_real_distribution<_RealType>::param_type&) [with _UniformRandomNumberGenerator = std::linear_congruential_engine<long unsigned int, 48271, 0, 2147483647>; _RealType = long double; std::uniform_real_distribution<_RealType>::result_type = long double] <near match>
operator()(_UniformRandomNumberGenerator& __urng,
^~~~~~~~
/usr/include/c++/7/bits/random.h:1818:2: note: passing ‘const std::uniform_real_distribution<long double>*’ as ‘this’ argument discards qualifiers

有人知道这里有什么问题吗?

最佳答案

std::uniform_real_distribution::operator() 不是常量成员函数,因此不能为常量实例调用:[rand.dist.uni.real] .


为什么它可以在 Windows 上运行? Microsoft 的operator() 可能是const,IIRC 实现可以加强成员函数的const 规范:http://eel.is/c++draft/member.functions#2 .

关于c++ - uniform_real_distribution operator() 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263255/

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