错误:表达式不能用作函数
error: expression cannot be used as a function
有my.hpp :
#include <utility>
#ifndef ROBBINS_MONRO
#define ROBBINS_MONRO
template <class Func, class DetermSeq, class RandomV, class RNG>
std::pair<double,double> robbins_monro(const Func & h, double x_init, double alpha, const DetermSeq & epsilon, RandomV & U, RNG & G, long unsigned N){
for(int i=0; i<N; i++){
x_init-= epsilon(i+1)(h(x_init) - alpha + U(G));
}
return std::make_pair(x_init, h(x_init));
}
#endif
我的测试1a.cpp:
#include <cmath>
#include <ctime>
#include <random>
#include "robbinsmonro.hpp"
#include <iostream>
int main() {
auto h = [](double x) { return 1/(1+exp(-x)); };
double alpha = 2/3;
std::uniform_real_distribution<double> U(-0.1,0.1);
double x_init=0;
auto epsilon = [] (long unsigned n) { return 1/(1+n); };
std::mt19937 G(time(NULL));
std::pair<double,double> a,b;
a = robbins_monro(h, x_init, alpha, epsilon, U, G, 1000000);
b = robbins_monro(h, x_init, alpha, epsilon, U, G, 10000000);
std::cout << "Pour N= 1000000, on obtient x= " << std::get<0>(a) << " (et h(x)= " << std::get<1>(b) << ")" << std::endl;
std::cout << "Pour N= 10000000, on obtient x= " << std::get<0>(b) << " (et h(x)= " << std::get<1>(b) << ")" << std::endl;
return 0;
}
我有这个错误:
错误:表达式不能用作函数
对于行:
x_init-= ε(i+1((h(x_init( - alpha + U(G((;
我不明白,因为所有术语都是双重的,而不是"函数">
我假设你想在这里做一个乘法,为此你需要*
运算符:
x_init -= epsilon(i+1) * (h(x_init) - alpha + U(G));
// ^^^
您编写的表达式在通常的代数中有意义,但在 c++ 中,第二个括号成为对epsilon(i+1)
结果的函数调用。由于这返回了一个unsigned long long
,它不是一个函数,你不能调用它。
此外,在此 lambda 中:
[] (long unsigned n) { return 1/(1+n); };
除非n
0
,否则这个函数将永远返回0
,因为你正在做整数除法。相反,您可以执行以下操作:
[] (long unsigned n) { return 1. /(1+n); };
相关文章:
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 为什么虚函数不能是静态的和全局的?
- 为什么继承的受保护构造函数不能公开?
- C++为什么类成员函数不能重新声明,但普通函数可以
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- 成员函数不能为集合迭代器和const_iterator的输入重载(但可以为其他 STL 迭代器重载)
- 为什么模板函数不能作为模板模板参数传递?
- 为什么 free 函数不能在 C 数组上运行,而 std::begin 在某些情况下可以在 C++14 中运行?
- 构造函数不能用于启发性化
- C 错误 - 函数不能超载
- 有没有办法使成员函数不能从构造函数调用
- 为什么模板函数不能是模板类的友元模板函数
- "Framework::Graphics::Material"的默认构造函数不能被引用 - 它是一个已删除的函数
- lower_bound()函数不能在调试模式下编译
- 为什么函数不能正确强制转换指针(从基类到派生类)
- 显式默认函数不能声明为 constexpr,因为隐式声明不是 constexpr
- 琐碎的默认构造函数不能是 constexpr?
- 函数不能嵌套,那么如何在C++中执行此操作
- 错误:成员函数不能在其类之外声明
- 错误-仅返回类型不同的函数不能重载.c++