Function As Parameter

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
asyukri10
New poster
Posts: 5
Joined: Wed Nov 28, 2001 2:00 am
Location: Malaysia
Contact:

Post by asyukri10 »

Hi,
Another help plz... How to send a function/module as parameter? I know to do it in C but how in C++? :|

My code(C),

#include <stdio.h>

void print(char* s)
{
printf("Print = %s",s);
}
//------------------------------------

void process(void(*k)())
{
(*k)("C++ or C?n");
}
//------------------------------------

void main()
{
process(print);
}
//------------------------------------


Visual C++ error if written as C++ code(.cpp),

--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
d:rubishdfcpp1.cpp(11) : error C2197: 'void (__cdecl *)(void)' : too many actual parameters
d:rubishdfcpp1.cpp(16) : error C2664: 'process' : cannot convert parameter 1 from 'void (char *)' to 'void (__cdecl *)(void)'
None of the functions with this name in scope match the target type
Error executing cl.exe.

Cpp1.exe - 2 error(s), 0 warning(s)
asyukri10
New poster
Posts: 5
Joined: Wed Nov 28, 2001 2:00 am
Location: Malaysia
Contact:

Post by asyukri10 »

void process(void(*k)(char*))
{ (*k)("C++ or C?n"); }
//------------------------------------
PMNOX
New poster
Posts: 49
Joined: Wed Feb 13, 2002 2:00 am
Location: Poland
Contact:

Post by PMNOX »

i didn't know that it is possible
Post Reply

Return to “C++”