C ++错误:没有匹配的调用函数 [英] C++ Error: no matching function for call

查看:195
本文介绍了C ++错误:没有匹配的调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用对分法求解二次方程。尝试求根时,出现以下错误:没有匹配的调用函数。

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;
}

错误发生在root = ...似乎有问题我的功能是f但我不明白这是怎么回事。以下两段代码是我的类和类实现文件。我们刚刚开始使用类,因此不确定我的问题是否在上面或仅在上面的代码中。

The error occurs where root = ... it seems to have a problem with my function f but I don't understand what is wrong. 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);
}


推荐答案

如果要使用指针

Change

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

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

在声明和定义中。

更改来自

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

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;
 }

这篇关于C ++错误:没有匹配的调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆