c++ - 不匹配调用 '(const Y) (int&, std::mersenne_twister_engine
有我的 .hpp :
#include <random>
#include <ctime>
#include <cmath>
#ifndef RECUIT_HPP
#define RECUIT_HPP
templat
问题描述
有我的 .hpp :
#include <random>
#include <ctime>
#include <cmath>
#ifndef RECUIT_HPP
#define RECUIT_HPP
template < class E, class Func, class TempSeq, class RandomY, class RNG>
E recuit_simule(const Func & phi, E x0, const TempSeq & T, const RandomY & Y, RNG & G, long unsigned N) {
std::uniform_real_distribution<double> U(0,1);
E y;
double u;
for(int i=0; i<N; i++) {
y=Y(x0, G);
u=U(G);
if(u <= fmin(1, exp((phi(x0) - phi(y))/T(N)))) {
x0=y;
}
}
return x0;
}
#endif
和我的 .cpp :
#include "recuit.hpp"
#include <iostream>
class Y {
private:
std::normal_distribution<double> N;
public:
Y() : N(0,1) {}
double operator()(const double & x, std::mt19937 & G) { return x + N(G); }
};
int main() {
auto phi=[](const double & x) { return x*x*x*x*x*x - 48*x*x; };
auto T=[] (long unsigned n) { return 10 * pow(0.9, n); };
Y A;
std::mt19937 G;
double x = recuit_simule(phi, 0, T, A, G, 1000);
std::cout << x << std::endl;
return 0;
}
当我编译我的 .cpp 时,我的 .hpp 中有以下错误:
recuit.hpp:17:6: 错误:不匹配调用 '(const Y) (int&, std::mersenne_twister_engine&)'</p>
对于该行:
y=Y(x0, G);
我不明白为什么
中国人使用农历。所以你可以在 python 中使用这样的库:
pip 安装 LunarCalendar
import datetime
from lunarcalendar import Converter, Solar, Lunar, DateNotExist
l = Lunar(year=2020, month=1, day=1, isleap=False)
print(Converter.Lunar2Solar(l))
返回规范 2020-01-25
解决方案
Y::operator()
并非const
如此,您不能在 const 对象上调用它。所以使参数Y
可变:
E recuit_simule(const Func & phi, E x0, const TempSeq & T, RandomY & Y, RNG & G, long unsigned N) {
// ^~~~~~~~~~~
// not const
旁注:您的代码非常混乱且难以阅读,因为您没有对类型和变量使用不同的表示法。例如Y
,是一个类型,也是一个参数的名称。而且您并不一致:有时变量是小写的,有时是大写的。
推荐阅读
- c++ - 如何在 centos 中使用更新选项?
- python - 累计配电线路
- excel-formula - 间接公式 = 间接(H9&"1:1")
- reactjs - 如何使用 Cookie 进行存储 jwt 令牌用户认证?
- php - 如何在 WooCommerce 的结帐页面上禁用更改运输方式的功能
- javascript - Cordova "Origin: file://"-Error using and Android 10, old versions working
- mysql - 如何多次选择一行,每行附加不同的值?
- javascript - try/catch 块中的多个依赖 API 调用?(NodeJS)
- c# - 尝试删除租用的 blob 时收到错误“当前 blob 上存在租约且未指定租约 ID”?
- amazon-web-services - Kubernetes Ingress Controller - 它有什么需求?
有我的 .hpp :
#include <random>
#include <ctime>
#include <cmath>
#ifndef RECUIT_HPP
#define RECUIT_HPP
templat
问题描述
有我的 .hpp :
#include <random>
#include <ctime>
#include <cmath>
#ifndef RECUIT_HPP
#define RECUIT_HPP
template < class E, class Func, class TempSeq, class RandomY, class RNG>
E recuit_simule(const Func & phi, E x0, const TempSeq & T, const RandomY & Y, RNG & G, long unsigned N) {
std::uniform_real_distribution<double> U(0,1);
E y;
double u;
for(int i=0; i<N; i++) {
y=Y(x0, G);
u=U(G);
if(u <= fmin(1, exp((phi(x0) - phi(y))/T(N)))) {
x0=y;
}
}
return x0;
}
#endif
和我的 .cpp :
#include "recuit.hpp"
#include <iostream>
class Y {
private:
std::normal_distribution<double> N;
public:
Y() : N(0,1) {}
double operator()(const double & x, std::mt19937 & G) { return x + N(G); }
};
int main() {
auto phi=[](const double & x) { return x*x*x*x*x*x - 48*x*x; };
auto T=[] (long unsigned n) { return 10 * pow(0.9, n); };
Y A;
std::mt19937 G;
double x = recuit_simule(phi, 0, T, A, G, 1000);
std::cout << x << std::endl;
return 0;
}
当我编译我的 .cpp 时,我的 .hpp 中有以下错误:
recuit.hpp:17:6: 错误:不匹配调用 '(const Y) (int&, std::mersenne_twister_engine&)'</p>
对于该行:
y=Y(x0, G);
我不明白为什么
中国人使用农历。所以你可以在 python 中使用这样的库:
pip 安装 LunarCalendar
import datetime
from lunarcalendar import Converter, Solar, Lunar, DateNotExist
l = Lunar(year=2020, month=1, day=1, isleap=False)
print(Converter.Lunar2Solar(l))
返回规范 2020-01-25
解决方案
Y::operator()
并非const
如此,您不能在 const 对象上调用它。所以使参数Y
可变:
E recuit_simule(const Func & phi, E x0, const TempSeq & T, RandomY & Y, RNG & G, long unsigned N) {
// ^~~~~~~~~~~
// not const
旁注:您的代码非常混乱且难以阅读,因为您没有对类型和变量使用不同的表示法。例如Y
,是一个类型,也是一个参数的名称。而且您并不一致:有时变量是小写的,有时是大写的。
推荐阅读
- c++ - 如何在 centos 中使用更新选项?
- python - 累计配电线路
- excel-formula - 间接公式 = 间接(H9&"1:1")
- reactjs - 如何使用 Cookie 进行存储 jwt 令牌用户认证?
- php - 如何在 WooCommerce 的结帐页面上禁用更改运输方式的功能
- javascript - Cordova "Origin: file://"-Error using and Android 10, old versions working
- mysql - 如何多次选择一行,每行附加不同的值?
- javascript - try/catch 块中的多个依赖 API 调用?(NodeJS)
- c# - 尝试删除租用的 blob 时收到错误“当前 blob 上存在租约且未指定租约 ID”?
- amazon-web-services - Kubernetes Ingress Controller - 它有什么需求?