Trying to call a member function through a pointer

B

Bruce.

I am trying to call a member function through a pointer to it and can't get
the syntax exactly right. This compiles with an error C2064, term does not
evaluate to a function taking 1 argument.. Here is the smallest code that
will not compile:

class CTest {
void function( char* param );
};

void CTest::function( char* param )
{
// Do something with parm;
}

void ( CTest::*pFunction )( char* param );

int _tmain(int argc, _TCHAR* argv[])
{
char* value;

pFunction( value ); // **** C2064
}

I just can't figure out the right syntax. Can anyone help me? Thanks!

Bruce.
 
I

Ian Collins

Bruce. said:
I can't figure out from that how to change my code to compile. It say a
member function is:

Its type is "int (Fred::*)(char,float)" if a non-static member function of
class Fred

And that is exactly the way I have it declared. What am I doing wrong?
What do I need to do differently?
Hard to tell without the context!

Ah, looking at your original post I see you are attempting to call the
member function without an instance of the class to call it on. You
can't do that.
 
O

osmium

Bruce. said:
I am trying to call a member function through a pointer to it and can't get
the syntax exactly right. This compiles with an error C2064, term does not
evaluate to a function taking 1 argument.. Here is the smallest code that
will not compile:

class CTest {
void function( char* param );
};

void CTest::function( char* param )
{
// Do something with parm;
}

void ( CTest::*pFunction )( char* param );

int _tmain(int argc, _TCHAR* argv[])
{
char* value;

pFunction( value ); // **** C2064
}

I just can't figure out the right syntax. Can anyone help me? Thanks!

Does this do what you want?

#include <iostream>

#define STOP while(1) cin.get();

using namespace std;

class C
{
public:
void f(char* cp) { cout << *cp << endl; }
};
typedef void (C::*PF)(char*);
//================
int main()
{
C c;
PF pf;
pf = &C::f;
char ch = 'A';
(c.*pf)(&ch);
STOP;
}
 
B

Bruce.

Ian Collins said:
Ah, looking at your original post I see you are attempting to call the
member function without an instance of the class to call it on. You
can't do that.

Ok, it looks like I oversimplied my example. In the original program, I am
trying to call one member of a class from another member of the same class
through a pointer. Function1, function2, and the function pointer are all
members of the same class. Here is an expanded one that resolves your point
yet produces the exact same error as before.

class CTest {
void function1();
void function2( char* param );
};

void CTest::function1( )
{
char value;
void ( CTest::*pFunction )( char* param );

pFunction = function2;
pFunction( value ); //*************** C2064;
}

int _tmain(int argc, _TCHAR* argv[])
{
char* value;

CTest test;
test.function1;
};

You can see from my example what I'm TRYING to do. How can I accomplish it?

Bruce.
 
T

Thomas J. Gritzan

Bruce. said:
Ok, it looks like I oversimplied my example. In the original program, I am
trying to call one member of a class from another member of the same class
through a pointer. Function1, function2, and the function pointer are all
members of the same class. Here is an expanded one that resolves your point
yet produces the exact same error as before.

class CTest {
public:

void function1();
void function2( char* param );
};

void CTest::function1( )
{
char value;

char* value;
void ( CTest::*pFunction )( char* param );

pFunction = function2;
pFunction( value ); //*************** C2064;
}

C2064? What error message?
Oh, google says this:
http://msdn2.microsoft.com/en-us/library/z72c1dfd(vs.80).aspx

You call a member function pointer without an object.
You need either the operator .* or ->*

Try:
this->*pFunction( value );
int _tmain(int argc, _TCHAR* argv[])

int main(int argc, char* argv[])
 
I

Ian Collins

Bruce. said:
Ok, it looks like I oversimplied my example. In the original program, I am
trying to call one member of a class from another member of the same class
through a pointer. Function1, function2, and the function pointer are all
members of the same class. Here is an expanded one that resolves your point
yet produces the exact same error as before.

class CTest {
void function1();
void function2( char* param );
};

void CTest::function1( )
{
char value;
void ( CTest::*pFunction )( char* param );

pFunction = function2;
pFunction( value ); //*************** C2064;

(this->*pFunction)( &value );

Note the object instance.
}

int _tmain(int argc, _TCHAR* argv[])

Why not

int main(int argc, char* argv[])
 
B

Bruce.

osmium said:
Does this do what you want?

#include <iostream>

#define STOP while(1) cin.get();

using namespace std;

class C
{
public:
void f(char* cp) { cout << *cp << endl; }
};
typedef void (C::*PF)(char*);
//================
int main()
{
C c;
PF pf;
pf = &C::f;
char ch = 'A';
(c.*pf)(&ch);
STOP;
}

Thank you! That gives me some important clues. I was beginning to think
this wasn't possible.

Bruce.
 
T

terminator

I am trying to call a member function through a pointer to it and can't get
the syntax exactly right. This compiles with an error C2064, term does not
evaluate to a function taking 1 argument.. Here is the smallest code that
will not compile:

class CTest {
void function( char* param );

};

void CTest::function( char* param )
{
// Do something with parm;

}

void ( CTest::*pFunction )( char* param );

int _tmain(int argc, _TCHAR* argv[])
{
char* value;
add this:
pFunction=CTest::function;
pFunction( value ); // **** C2064
correct the line in this way:
Ctest ct;
ct.*pFunction( value )
}

I just can't figure out the right syntax. Can anyone help me? Thanks!

Bruce.

regards ,
FM
 
J

James Dennett

terminator said:
I am trying to call a member function through a pointer to it and can't get
the syntax exactly right. This compiles with an error C2064, term does not
evaluate to a function taking 1 argument.. Here is the smallest code that
will not compile:

class CTest {
void function( char* param );

};

void CTest::function( char* param )
{
// Do something with parm;

}

void ( CTest::*pFunction )( char* param );

int _tmain(int argc, _TCHAR* argv[])
{
char* value;
add this:
pFunction=CTest::function;

That's a syntax error. It's necessary to take the address
explicitly:
pFunction = &CTest::function;
though some older compilers get that wrong.

-- James
 
A

Asm23

I have modify the code below!
//=================================================================
class CTest {
public:
static void function( char* param );

};


void CTest::function( char* param )
{
// Do something with parm;

}
static void ( CTest::*pFunction )( char* param );

void main()
{
char* value;

CTest::function(value);//OK! pass
pFunction( value ); // **** C2064 error

}

//=============================================================

I wonder why the expression "CTest::function(value);" is OK?
But the next sentence still has compling error?
Why?
 
F

Francis Glassborow

Asm23 said:
I have modify the code below!
//=================================================================
class CTest {
public:
static void function( char* param );

};


void CTest::function( char* param )
{
// Do something with parm;

}
static void ( CTest::*pFunction )( char* param );

That declares pFunction to be a pointer to a member of CTest that is a
member function that takes a char * parameter and returns nothing.

Note that you have not initialised pFunction so using it is an error
void main()

Int main() please
{
char* value;

CTest::function(value);//OK! pass
Well that is perfectly good syntax for calling a static member function
pFunction( value ); // **** C2064 error

but what on earth do you think that would do?
 
A

Asm23

That declares pFunction to be a pointer to a member of CTest that is a
member function that takes a char * parameter and returns nothing.

Note that you have not initialised pFunction so using it is an error




Int main() please> {


Well that is perfectly good syntax for calling a static member function


but what on earth do you think that would do?


How can I define a function pointer point to a static member funcion?
My first statement "CTest::function(value);" is the only method?
 
V

Victor Bazarov

Asm23 said:
How can I define a function pointer point to a static member funcion?

It's just a function pointer:

char* (*pf)() = &CTest::function;
My first statement "CTest::function(value);" is the only method?

No. Your "CTest::function(value);" statement is not a declaration, it's
a function call.

V
 
O

osmium

Asm23 said:
How can I define a function pointer point to a static member funcion?
My first statement "CTest::function(value);" is the only method?

This works and uses your code as a base.

#include <iostream>
//=================================================================
class CTest {
public:
static void function( char* param );


};


void CTest::function( char* param )
{
// Do something with parm;
std::cout << param << std::endl;

}
//static void ( CTest::*pFunction )( char* param );

typedef void (*PF)(char*);

int main()
{
PF pf = CTest::function;

//CTest::function(value);//OK! pass
pf("hello");
// pFunction( value ); // **** C2064 error
//getchar();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top