Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function Pointers!?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

Can someone tell me what I'm doing wrong in the following code? The code may appear somewhat useless and that would be almost true. The code is actually a simpler version of another set of more usefull code. By breaking down the original code, I had hoped to simplify the troubleshooting process. But I was unable to find a solution this way, I still get an error "Call of non function" (point of error is indicated in code) whenever I try to compile either sets of code. I appreciate any help offered, thanks in advance :)


//Header File
#ifndef functioncaller_h
#define functioncaller_h

class FunctionCaller
{
struct Tag
{
char* fname;
int (FunctionCaller::*fptr)(void);
};

Tag Tags[5];

int DoFunction1(void);
int DoFunction2(void);
int DoFunction3(void);
int DoFunction4(void);

public:
FunctionCaller(void);

int CallFunction(char* func);
};


#endif

//---------------------------------------------------------------------
//CPP file
#include <string.h>
#include <stdlib.h>
#include &quot;functioncaller.h&quot;


FunctionCaller::FunctionCaller(void)
{
//Define Tags
Tags[0].fname = &quot;Function1&quot;;
Tags[0].fptr = &FunctionCaller::DoFunction1;

Tags[1].fname = &quot;Function2&quot;;
Tags[1].fptr = &FunctionCaller::DoFunction2;

Tags[2].fname = &quot;Function3&quot;;
Tags[2].fptr = &FunctionCaller::DoFunction3;

Tags[3].fname = &quot;Function4&quot;;
Tags[3].fptr = &FunctionCaller::DoFunction4;

Tags[4].fname = NULL;
Tags[4].fptr = NULL;

}

int FunctionCaller::CallFunction(char* func)
{
//Match func with a Tag
for (int i = 0; Tags.fname != NULL; i++)
{
if ( strncmp(Tags.fname, func, strlen(Tags.fname) ) ==0)
return ( (Tags.fptr)() ); //&quot;Call of non function&quot; error occurs on this line
}

//Function name not found, so exit
exit(1);
}
 
Your functions are not static and because of that I believe that you may actually have to declare a class. I dont remember exactly as I haven't worked with method pointers in a while but here is some code I set asside to remind me how to do it.

Matt


class c1{
public:
c1::c1(){}
void sample1();
void sample2();
void sample3();
void sample4();
void sample5();
void sample6();

};

void c1::sample1(){cout<<&quot;sample1\n&quot;;}
void c1::sample2(){cout<<&quot;sample2\n&quot;;}
void c1::sample3(){cout<<&quot;sample3\n&quot;;}
void c1::sample4(){cout<<&quot;sample4\n&quot;;}
void c1::sample5(){cout<<&quot;sample5\n&quot;;}
void c1::sample6(){cout<<&quot;sample6\n&quot;;}

class c2{
public:
c2::c2( void(c1::*ptr)() ):method_ptr(ptr){};
void setmethod(void(c1::*ptr)());
void fire();
private:

void (c1::*method_ptr)();
};


void c2::fire(){c1 temp;(temp.*method_ptr)();}
void c2::setmethod(void(c1::*ptr)()){method_ptr=ptr;}


int main()
{
c2 mc2(&c1::sample1);

mc2.fire();
mc2.setmethod(&c1::sample2);
mc2.fire();
mc2.setmethod(&c1::sample3);
mc2.fire();
mc2.setmethod(&c1::sample4);
mc2.fire();
mc2.setmethod(&c1::sample5);
mc2.fire();
mc2.setmethod(&c1::sample6);
mc2.fire();
}
 
By looking at my old code, it seems that you are calling the functions but there is no memory allocated for them. Try declaring a class and using that class .* <--- (yes it looks weird)


Hope that sample above helps..

Matt
 
Puuh... tricky. Okay, what's the situation here?
You are trying to take the address of a member
function and call it at runtime (using this
address). The compiler refuses to do so, and
the question is: Why?

Someone who has more detailed knowledge about
C++ compilers may explain it. For all I know
it is impossible to call (the address of) a
member function inside of the class itself.

Solution:
- Declare the Do...() functions as &quot;static&quot;.
- Change &quot;int (FunctionCaller::*fptr)(void);&quot;
to &quot;int (*fptr)(void);&quot;

I guess the problem is that the adress of a
member function is unknown at compile-time as
long as it is non-static but I am not 100% sure.

-- ralf


 
I've changed a little your code:
#include <string.h>
#include <stdlib.h>
#include<string>
using namespace std;
class FunctionCaller
{
struct Tag
{
string fname;
int (FunctionCaller::*fptr)(void);
};

Tag Tags[5];

int DoFunction1(void){return 0;}
int DoFunction2(void){return 0;}
int DoFunction3(void){return 0;}
int DoFunction4(void){return 0;}

public:
FunctionCaller(void);
int CallFunction(char* func);
};
//CPP file


FunctionCaller::FunctionCaller(void)
{
//Define Tags
Tags[0].fname = &quot;Function1&quot;;
Tags[0].fptr = FunctionCaller::DoFunction1;

Tags[1].fname = &quot;Function2&quot;;
Tags[1].fptr = FunctionCaller::DoFunction2;

Tags[2].fname = &quot;Function3&quot;;
Tags[2].fptr = FunctionCaller::DoFunction3;

Tags[3].fname = &quot;Function4&quot;;
Tags[3].fptr = FunctionCaller::DoFunction4;

Tags[4].fname = &quot;&quot;;
Tags[4].fptr = NULL;

}

int FunctionCaller::CallFunction(char* func)
{
//Match func with a Tag
for (int i = 0; Tags.fname != &quot;&quot;; i++)
{
if (Tags.fname==func)
{
return (this->*Tags.fptr)();
}
}
return 0;
//Function name not found, so exit
}
John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top