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 25097 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: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |