对 '(const Y) (int&, std::mersenne_twister_engine<long unsigned int,
no match for call to ‘(const Y) (int&, std::mersenne_twister_engine<long unsigned int,
有我的.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&('
对于行:
y=Y(x0, G);
我不明白为什么
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
既是类型,也是参数的名称。而且你不一致:有时变量是小写的,有时是大写的。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 在c++中访问int到类对象的映射时出错
- 为什么我无法更改"set<set>"循环中的值<int>
- 长 长 int 不要 长 int 好
- C++程序在循环后给出奇怪的int值
- 如何计算数据类型的范围,例如int
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)