关于函数指针的问题

lokibalder 2009-02-15 03:56:08

class A{
public:
A();
~A();
int process(int i);
};

A::A(){
}

A::~A(){
}

int A::process(int i){
return i*2;
}

void call_func_pointer(int (A::*func)(int)){
func(1);
//报错!,term does not evaluate to a function
}

int main(){
A a;
call_func_pointer(a.process);//传入函数指针
return 0;
}

请问怎么解决这个错误?
...全文
139 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang_zhigang_26 2009-02-15
  • 打赏
  • 举报
回复
class 的member function 是不能够直接调用的,调用时必须与一个对象实例绑定。所以你的code大概应该这样写
class A{
public:
A();
~A();
int process(int i);
};

A::A(){
}

A::~A(){
}

int A::process(int i){
return i*2;
}

void call_func_pointer(int (A::*func)(int)){
A a;
(a.*func)
(1);
//报错!,term does not evaluate to a function
}

int main(){
A a;
call_func_pointer(a.process);//传入函数指针
//这里我也觉得不太对,因为你需要取指针,不过编译器不同可能可以通过。
//我的倾向是 call_func_pointer(&A::process);
return 0;
}
hmt.sysfixer.net 2009-02-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lokibalder 的回复:]
引用 6 楼 feng4206yu 的回复:
C/C++ code
#include <iostream>
using namespace std;

class A
{
public:
int fun1(int i)
{
return i;
};
};

template <class T>
void fun2(int j, T *obp, int (T::*p)(int)){
cout < <(obp->*p)(j);
}

void main()
{
int (A::*fp1)(int);
fp1=&A::fun1;
A oba;
A *…
[/Quote]

有问题的
fp1=&A::fun1; //A::fun1本身就是地址,为什么还要取地址?
KHacker_001 2009-02-15
  • 打赏
  • 举报
回复
不会 呵呵 学习者
fengjian_net 2009-02-15
  • 打赏
  • 举报
回复
外部函数使用函数指针调用类成员函数,有两种解决办法,一种是在类中定义该成员函数指针,另一种更简单,即将类成员函数定义为静态函数。

class A{
public:
A();
~A();
static int process(int i);
};

A::A(){
}

A::~A(){
}

int A::process(int i){
return i*2;
}

void call_func_pointer(int (*func)(int)){
func(1);

}

int main(){
A a;
call_func_pointer(A::process);//传入函数指针
return 0;
}
lokibalder 2009-02-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 feng4206yu 的回复:]
C/C++ code
#include <iostream>
using namespace std;

class A
{
public:
int fun1(int i)
{
return i;
};
};

template <class T>
void fun2(int j, T *obp, int (T::*p)(int)){
cout <<(obp->*p)(j);
}

void main()
{
int (A::*fp1)(int);
fp1=&A::fun1;
A oba;
A *ob…
[/Quote]

这样啊,看来比较麻烦,算分了。
feng4206yu 2009-02-15
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

class A
{
public:
int fun1(int i)
{
return i;
};
};

template <class T>
void fun2(int j, T *obp, int (T::*p)(int)){
cout <<(obp->*p)(j);
}

void main()
{
int (A::*fp1)(int);
fp1=&A::fun1;
A oba;
A *obap=&oba;
int i=1;
fun2(i,obap,fp1);
}
lokibalder 2009-02-15
  • 打赏
  • 举报
回复
如果我想用类似传入全局函数指针的方法,传入a的成员函数指针,然后调用,该怎么实现?
我题目中的代码,指针是传入了。格式正确,但是不能像我3L写的代码那样调用这个函数,这是为什么?应该怎么改?
waizqfor 2009-02-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lokibalder 的回复:]
如果我想传入A的成员函数,那么该怎么调用?

如果我改称这样,传入全局函数,是可以的:
int process(int i){
return i*2;
}

void call_func_pointer(int (*func)(int)){
func(1);
//正常
}

int main(){
call_func_pointer(process);//传入函数指针
return 0;
}
[/Quote]
定义一个类的指针 用指针去调用
lokibalder 2009-02-15
  • 打赏
  • 举报
回复
如果我想传入A的成员函数,那么该怎么调用?

如果我改称这样,传入全局函数,是可以的:
int process(int i){
return i*2;
}

void call_func_pointer(int (*func)(int)){
func(1);
//正常
}

int main(){
call_func_pointer(process);//传入函数指针
return 0;
}
lokibalder 2009-02-15
  • 打赏
  • 举报
回复
如果我想传入A的成员函数,那么该怎么调用?

如果我改称这样,传入全局函数,是可以的:
int process(int i){
return i*2;
}

void call_func_pointer(int (*func)(int)){
func(1);
//正常
}

int main(){
call_func_pointer(process);//传入函数指针
return 0;
}
waizqfor 2009-02-15
  • 打赏
  • 举报
回复
func函数没有被声明

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧