C++错误:没有用于调用的匹配函数
C++ 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似乎有问题,但我不明白出了什么问题。以下两位代码是我的类和类实现文件。我们刚开始处理类,所以我不确定我的问题是存在还是仅仅存在于上面的代码中。
#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);
}
第四个参数是一个函数指针,
double bisect(double, double, double, double f(double), bool&);
当您调用此函数时:
root = s.bisect (s.xLeft, s.xRight, s.epsilon, s.f, s.error);
虽然成员虚构double f(double)
与该参数的类型不同,因为这是C++成员函数,而不是静态的,所以在编译时添加了"this"参数作为该成员函数。
键入将静态关键字添加到函数中。
函数指针的语法通常为:double (*f)(double)
。除此之外,您还试图通过非成员函数指针传递成员函数。由于您的函数不使用任何成员变量,因此最简单的解决方案是将其设为static
:
class solution {
// ...
static double f(double);
};
如果要使用指向成员函数的指针。
更改
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);
这就是我所拥有的,为我成功编译和链接。
#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;
}
我认为这与回调函数有关。通常,当使用不正确的函数调用时,会出现这种编译器错误。如果您想要这种回调函数,您可能需要查看函数指针。
http://www.cprogramming.com/tutorial/function-pointers.html
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销