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.. 5 25100 st***********@vanderbeke.be napsal:
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..
You are trying to pass pointer to member of class as parameter. It is
something different, than pointer to ordinary function. But you can
still pass pointer to static member function - it works same way as
ordinary function, because it does not need 'this' value for invocation.
On 27 Nov 2006 04:47:26 -0800, steven_orocos wrote:
>I'm tryin to pass a funtion as an argument in another funtion. But when I put the cide in a class, it won't work anymore. Also the net gives me several different solution, but none work..
Try this: http://www.newty.de/fpt/index.html
Good luck,
Roland Pibinger st***********@vanderbeke.be wrote:
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..
Pointers to members differ from pointers to ordinary functions
(and one cannot be cast to another), because member functions take
an extra "hidden" 'this' argument.
HTH,
- J.
Roland Pibinger schreef:
Try this: http://www.newty.de/fpt/index.html
I had allready found that link
using this code
test3(*this.*test1);
I get error: '((MyClass*)this)->MyClass::test1' cannot be used as a
member pointer, since it is of type '<unknown type>'
And this
test3((void(*)(int))*this.*test1);
gives me
error: invalid cast from type 'MyClass' to type 'void (*)(int)'
On 27 Nov 2006 04:47:26 -0800 in comp.lang.c++, st***********@vanderbeke.be wrote,
>-------------------------------- But when I put the cide in a class, it won't work anymore. ------------------------------- typedef void (*func)(int);
typedef void (MyClass::*func)(int);
>void MyClass::test1(int a){cout<<"1";} void MyClass::test2(int a){cout<<"2";} void MyClass::test3 (void (*fp) (int)) {
void MyClass::test3 (func fp) {
> (fp)(5);
(this->*fp)(5);
>} void MyClass::test() { func pf; pf = test1;
pf = &test1;
> test3((func)test1);
test3(&test1);
This issue is covered in Marshall Cline's C++ FAQ. See the section
"[33.5] How can I avoid syntax errors when calling a member function
using a pointer-to-member-function? " but ignore his #define. It is
always good to check the FAQ before posting. You can get the FAQ at: http://www.parashift.com/c++-faq-lite/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Foxy Kav |
last post by:
Hi everyone, im a first year UNI student doing a programming subject
and im stuck on how to get rid of my global variables, char
stringarray and char emptystring. I was wondering if
anyone could...
|
by: Victor Bazarov |
last post by:
In the project I'm maintaining I've seen two distinct techniques used for
returning an object from a function. One is
AType function(AType const& arg)
{
AType retval(arg); // or default...
|
by: Mike MacSween |
last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime
ago.
Then remembered that arguments are passed by reference, by default.
Does the fact that this slowed me down...
|
by: Charles Sullivan |
last post by:
The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
If in my code I write:
int cmp_fcn(...);
int...
|
by: sd2004 |
last post by:
I am still learning, could someone
show/explain to me how to fix the error.
I can see it is being wrong but do not know how to fix.
could you also recommend a book that I can ref. to ?...
|
by: User1014 |
last post by:
Since you can pass a function to a ... erm...... function....
how to you use the result of a function as the argument for another
function instead of passing the actual function to it.
i.e.
...
|
by: dave_dp |
last post by:
Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.
When parameters are passed/returned...
|
by: sanjay |
last post by:
Hi,
I have a doubt about passing values to a function accepting string.
======================================
#include <iostream>
using namespace std;
int main()
{
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |