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)tes t1);
}
-----------------------
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 25130 st***********@v anderbeke.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)tes t1);
}
-----------------------
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***********@v anderbeke.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)tes t1);
}
-----------------------
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.*te st1);
I get error: '((MyClass*)thi s)->MyClass::test1 ' cannot be used as a
member pointer, since it is of type '<unknown type>'
And this
test3((void(*)( int))*this.*tes t1);
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***********@v anderbeke.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)tes t1);
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) {
document.images.src = eval("mt" +menu+ ".src")
}
alert("imgOff_hidemenu");
hideMenu=setTimeout('Hide(menu,num)',500);
|
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 code...
TCHAR myArray;
DoStuff(myArray);
|
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 show me a example of how to do this. I was told to pass
the arrays from function to function, but i do not know how ....
Thanxs if you can help.
My code (First two functions only - there's more):
|
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 construction and then..
// some other processing and/or changing 'retval'
return retval;
}
|
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 indicate:
a) That I don't know enough
b) Passing arguments by ref is bad
| |
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 (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);
then everything works. But if instead I code qsort as:
|
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 ?
////////////////// ERROR MESSAGE /////////////////////
bash-2.05b$ g++ test5c.cpp
test5c.cpp: In function `int main()':
test5c.cpp:36: error: invalid initialization of reference of type
|
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.
function foo2(){}
function foo(func){}
|
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 value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |