C++ 错误:没有匹配函数来调用‘simplex615<arbitraryFunc>::amoeba

标签 c++

我正在尝试实现一些 C++ 代码,以使用单纯形算法找到函数的最大值。不幸的是,我在 C++ 方面的经验为零。 我遇到了这个错误,似乎无法从类似问题的答案中找到解决方案。

simplex.cpp: In function ‘int main(int, char**)’:
simplex.cpp:22:25: error: no matching function for call to ‘simplex615::amoeba(arbitraryFunc&, double)’
simplex.amoeba(foo, 1e-7);

还有与链接文件“simplex615.h”相关的警告

In file included from simplex.cpp:4:0:
simplex615.h:302:6: note: candidate: void simplex615::amoeba(optFunc&, double) [with F = arbitraryFunc]
void simplex615 ::amoeba(optFunc& foo, double tol) {
simplex615.h:302:6: note: no known conversion for argument 1 from ‘arbitraryFunc’ to ‘optFunc&

单纯形.cpp

#include <vector>
#include <cmath>
#include <iostream>
#include "simplex615.h"
#define ZEPS 1e-10

// function object used as an argument
class arbitraryFunc {
    public:
    double operator() (std::vector<double>& x) {
        // f(x0,x1) = 100*(x1-x0^2)^2 + (1-x0)^2
        return 100*(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])+(1-x[0])*(1-x[0]);
    }
};

int main(int main, char** argv) {
    double point[2] = {-1.2, 1};
    arbitraryFunc foo;
    // initial point to start
    // WILL BE DISCUSSED LATER
    simplex615 <arbitraryFunc> simplex(point, 2); // create a simplex
    simplex.amoeba(foo, 1e-7);  // optimize for a function
    // print outputs
    std::cout << "Minimum = " << simplex.ymin() << ", at ("
    << simplex.xmin()[0] << ", " << simplex.xmin()[1]
    << ")" << std::endl;
    return 0;
}

simplex615.h

template <class F> // F is a function object
void simplex615 <F>::amoeba(optFunc& foo, double tol) {
  evaluateFunction(foo);
  while(true) {
    evaluateExtremes();
    prepareUpdate();

    if ( check_tol(Y[idxHi],Y[idxLo],tol) ) break;

    updateSimplex(foo, -1.0); // reflection

    if ( Y[idxHi] < Y[idxLo] ) {
      updateSimplex(foo, -2.0); // expansion
    }
    else if ( Y[idxHi] >= Y[idxNextHi] ) {
      if ( !updateSimplex(foo, 0.5) ) {
    contractSimplex(foo);
      }
    }
  }
}

simplex615.h

class optFunc {
   public:
      virtual double operator() (std::vector<double>& x) = 0;
};

完整文件 simplex.cpp 和 simplex.h 的链接:Source code

任何帮助将不胜感激。谢谢。

最佳答案

在我看来,在您的 simplex615.h 中,您忘记了在 amoeba 方法中使用“class F”。只需将 optFunc 替换为 F 即可解决问题。

template <class F> // F is a function object
void simplex615 <F>::amoeba(F& foo, double tol) {
...
}

C++ 中的模板类参数定义了一个在使用模板时可以替换的通用类型。

同样在这个例子中,您可以从头文件中删除 optFunc 的声明。

关于C++ 错误:没有匹配函数来调用‘simplex615<arbitraryFunc>::amoeba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54362898/

相关文章:

c++ - -->> 实际上是做什么的?

c++ - ls 命令在终端中可运行,在 C++ 中不可运行

c++ - 使用 ID 和数组( vector )的性能

c++ - QT QOverload 未在 Travis CI 的此范围内声明

c++ - 如何理解指针数组的地址?

c++ - C++ 中的非刚体 2D 物理引擎

c++ - 使用局域网中的计算机进行并行计算?

C++ 对 shared_ptr 的引用与引用

c++ - 对 `cvCreateBGCodeBookModel' 的 undefined reference

c++ - MS SQL 存储过程使用 ODBC 返回结果集