S
steven_orocos
Hi,
I'm tryin to pass a funtion as an argument in another funtion.
This code works
----------------------------------
typedef void (*func)(int);
void test1(int a){cout<<"1";}
void test2(int a){cout<<"2";}
void test3 (void (*fp) (int)) {
(fp)(5);
}
void test() {
func pf;
pf = test1;
test3(test1);
}
--------------------------------
But when I put the cide in a class, it won't work anymore.
-------------------------------
typedef void (*func)(int);
void MyClass::test1(int a){cout<<"1";}
void MyClass::test2(int a){cout<<"2";}
void MyClass::test3 (void (*fp) (int)) {
(fp)(5);
}
void MyClass::test() {
func pf;
pf = test1;
test3((func)test1);
}
-----------------------
Gives me the error
--------------
error: argument of type 'void (MyClass:
(int)' does not match
'void (*)(int)'
error: invalid use of member (did you forget the '&' ?)
--------------------
I've tried several things, like
test3(&test1);
or
typedef void (MyClass::*func)(int);
But i can't find the solution.
Also the net gives me several different solution, but none work..
I'm tryin to pass a funtion as an argument in another funtion.
This code works
----------------------------------
typedef void (*func)(int);
void test1(int a){cout<<"1";}
void test2(int a){cout<<"2";}
void test3 (void (*fp) (int)) {
(fp)(5);
}
void test() {
func pf;
pf = test1;
test3(test1);
}
--------------------------------
But when I put the cide in a class, it won't work anymore.
-------------------------------
typedef void (*func)(int);
void MyClass::test1(int a){cout<<"1";}
void MyClass::test2(int a){cout<<"2";}
void MyClass::test3 (void (*fp) (int)) {
(fp)(5);
}
void MyClass::test() {
func pf;
pf = test1;
test3((func)test1);
}
-----------------------
Gives me the error
--------------
error: argument of type 'void (MyClass:
'void (*)(int)'
error: invalid use of member (did you forget the '&' ?)
--------------------
I've tried several things, like
test3(&test1);
or
typedef void (MyClass::*func)(int);
But i can't find the solution.
Also the net gives me several different solution, but none work..