简体   繁体   English

C++ 错误:没有匹配的调用函数

[英]C++ Error: no matching function for call

I am trying to solve a quadratic equation using the bisection method.我正在尝试使用二分法求解二次方程。 When trying to evaluate the roots I get this error: "no matching function for call".尝试评估根时,我收到此错误:“没有匹配的调用函数”。

#include "assign4.h"
#include <iostream>

using namespace std;

int main(int argc, char * argv[]){
   solution s;
   double root;

   cout << "Enter interval endpoints: ";
   cin >> s.xLeft >> s.xRight;

   cout << "Enter tolerance: ";
   cin >> s.epsilon;

   root = s.bisect (s.xLeft, s.xRight, s.epsilon, s.f, s.error);

   if (!(s.error))
      cout << "Root found at " << root << "\nValue of f(x) at root is: " << s.f(root);
   else
      cout << "The solution of a quadratic equation with coefficients: " << endl;
      cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
      cout << "has not been found." << endl;

   return 0;
}

The error occurs where root = ... it seems to have a problem with my function f but I don't understand what is wrong.错误发生在 root = ... 我的函数 f 似乎有问题但我不明白出了什么问题。 The following two bits of code are my class and class implementation files.下面两段代码是我的类和类实现文件。 We just started working with classes so I am uncertain if my problem lies there or simply in the above code.我们刚刚开始使用类,所以我不确定我的问题是在那里还是只是在上面的代码中。

#ifndef ASSIGN4_H
#define ASSIGN4_H

class solution {

public:
   double xLeft, xRight;
   double epsilon;
   bool error;

   double bisect(double, double, double, double f(double), bool&);
   double f(double);
};
#endif // ASSIGN4_H

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////// ///////////////////////////////////////////////// ///////////////////////////////////////////////// ////////////

#include "assign4.h"
#include <iostream>
#include <cmath>

using namespace std;

double solution::bisect (double xLeft, double xRight, double epsilon, double func(double), bool& error) {
   double xMid;
   double fLeft, fRight;
   double fMid;

   fLeft = f(xLeft);
   fRight = f(xRight);

   error = (fLeft * fRight) > 0;
   if (error)
      return -999.0;

   while (fabs (xLeft - xRight) > epsilon) {
      xMid = (xLeft + xRight) / 2.0;
      fMid = f (xMid);

      if (fMid == 0.0)
         return xMid;
      else if (fLeft * fMid < 0.0)
         xRight = xMid;
      else
         xLeft = xMid;

      cout << "New Interval is [" << xLeft << ", " << xRight << "]" << endl;
   }

return (xLeft + xRight) / 2.0;
}

double solution::f (double x) {
   return ((5 * pow(x,2.0)) + (5 * x) + 3);
}

The 4th parameter is a function pointer,第四个参数是一个函数指针,

double bisect(double, double, double, double f(double), bool&);

When you call this function:当你调用这个函数时:

root = s.bisect (s.xLeft, s.xRight, s.epsilon, s.f, s.error);

While the member fiction double f(double) is not the same type as that parameter because this is C++ member function and not static, so the 'this' parameter is added this member function when compiling.而成员虚构double f(double)与该参数的类型不同,因为这是 C++ 成员函数而不是静态的,因此在编译时将 'this' 参数添加到该成员函数中。

type add the static key word to the function.键入将静态关键字添加到函数中。

The syntax for a function pointer is usually: double (*f)(double) .函数指针的语法通常是: double (*f)(double) Aside from that, you are attempting to pass a member function through a non-member-function pointer.除此之外,您还试图通过非成员函数指针传递成员函数。 Since your function does not use any member variables, the simplest solution would be to make it static :由于您的函数不使用任何成员变量,因此最简单的解决方案是将其设为static

class solution {
  // ...
  static double f(double);
};

If you want to use pointers to member functions.如果要使用指向成员函数的指针。

Change改变

double bisect(double, double, double, double f(double), bool&);

to

double bisect(double, double, double, double (solution::*f)(double), bool&);

in declaration and definition.在声明和定义中。

Change the call from更改呼叫从

root = s.bisect (s.xLeft, s.xRight, s.epsilon, s.f, s.error);

to

root = s.bisect (s.xLeft, s.xRight, s.epsilon, &solution::f, s.error);

This is what I have that compiles and links successfully for me.这就是我为我成功编译和链接的内容。

 #include <iostream>
 #include <typeinfo>
 #include <math.h>

 using namespace std;

 class solution {

 public:
    double xLeft, xRight;
    double epsilon;
    bool error;

    double bisect(double, double, double, double (solution::*f)(double), bool&);
    double f(double);
 };

 using namespace std;

 double solution::bisect (double xLeft, double xRight, double epsilon, double (solution::*func)(double), bool& error) {
    double xMid;
    double fLeft, fRight;
    double fMid;

    fLeft = (this->*func)(xLeft);
    fRight = (this->*func)(xRight);

    error = (fLeft * fRight) > 0;
    if (error)
       return -999.0;

    while (fabs (xLeft - xRight) > epsilon) {
       xMid = (xLeft + xRight) / 2.0;
       fMid = (this->*func)(xMid);

       if (fMid == 0.0)
          return xMid;
       else if (fLeft * fMid < 0.0)
       {
          xRight = xMid;
          fRight = fMid;
       }
       else
       {
          xLeft = xMid;
          fLeft = fMid;
       }

       cout << "New Interval is [" << xLeft << ", " << xRight << "]" << endl;
    }

 return (xLeft + xRight) / 2.0;
 }

 double solution::f (double x) {
    return ((5 * pow(x,2.0)) + (5 * x) + 3);
 }

 int main(int argc, char * argv[]){
    solution s;
    double root;

    cout << "Enter interval endpoints: ";
    cin >> s.xLeft >> s.xRight;

    cout << "Enter tolerance: ";
    cin >> s.epsilon;

    root = s.bisect (s.xLeft, s.xRight, s.epsilon, &solution::f, s.error);

    if (!(s.error))
       cout << "Root found at " << root << "\nValue of f(x) at root is: " << s.f(root) << endl;
    else
    {
       cout << "The solution of a quadratic equation with coefficients: " << endl;
       // cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
       cout << "has not been found." << endl;
    }
    return 0;
 }

I believe it has to do with your callback function.我相信这与您的回调函数有关。 Typically you get that kind of compiler error when you use an incorrect function call.通常,当您使用不正确的函数调用时,会出现这种编译器错误。 If you want this kind of callback function, you may want to look into function pointers.如果您想要这种回调函数,您可能需要查看函数指针。

http://www.cprogramming.com/tutorial/function-pointers.html http://www.cprogramming.com/tutorial/function-pointers.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM