C++问题关于函数指针

来源:百度知道 编辑:UC知道 时间:2024/05/09 04:06:28
//二分法解方程,想通过地址来计算值!也就是通过函数指针传递来解
//f(x)的值!

// bisection.h: interface for the Cbisection class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BISECTION_H__A062A1F0_7044_4D35_8028_FA7C70A54CC5__INCLUDED_)
#define AFX_BISECTION_H__A062A1F0_7044_4D35_8028_FA7C70A54CC5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class Cbisection
{
public:
float bisection(float (*fl)(float));
Cbisection();
virtual ~Cbisection();
float f(float x);

private:
float a;
float b;
float x;

};

#endif // !defined(AFX_BISECTION_H__A062A1F0_7044_4D35_8028_FA7C70A54CC5__INCLUDED_)

// bisection.cpp: implementation of the Cbisection class.
//
//////////////////////////////////////////////////////////////////////

#include "bisection.h"
#include

你的问题和this指针有关

有两个方法解决你的问题:
1.不将f做为Cbisection的成员函数,而将f做为一个全局函数:
float f(float x)
{
return x*x*x+x*x-3*x-3;
}

2.使用指向成员函数的指针做为biscetion函数的变量.这个你自己参考参考C++的书,我没有去查,不敢胡乱写上来.但是第一种方法应该奏效.

也不说问题,不知道你贴一段代码出来干嘛?

你应该说问题
再说你的、代码 也没有注释
看起来很麻烦的