By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Trying to call a member function through a pointer

P: n/a
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.
Jun 13 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a
Bruce. wrote:
I am trying to call a member function through a pointer to it and
can't get the syntax exactly right.
<http://www.parashift.com/c++-faq-lite/pointers-to-members.html>

Brian
Jun 13 '07 #2

P: n/a

"Default User" <de***********@yahoo.comwrote in message
news:5d*************@mid.individual.net...
Bruce. wrote:
>I am trying to call a member function through a pointer to it and
can't get the syntax exactly right.

<http://www.parashift.com/c++-faq-lite/pointers-to-members.html>
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?

Bruce.
Jun 13 '07 #3

P: n/a
Bruce. wrote:
"Default User" <de***********@yahoo.comwrote in message
news:5d*************@mid.individual.net...
>Bruce. wrote:
>>I am trying to call a member function through a pointer to it and
can't get the syntax exactly right.
<http://www.parashift.com/c++-faq-lite/pointers-to-members.html>

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.

--
Ian Collins.
Jun 13 '07 #4

P: n/a
"Bruce." writes:
>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;
}
Jun 13 '07 #5

P: n/a
"Ian Collins" <ia******@hotmail.comwrote in message
news:5d*************@mid.individual.net...
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.
Jun 14 '07 #6

P: n/a
Bruce. wrote:
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/lib...fd(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[])
{
char* value;

CTest test;
test.function1;
test.function1();
};
--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 14 '07 #7

P: n/a
Bruce. wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:5d*************@mid.individual.net...
>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;
(this->*pFunction)( &value );

Note the object instance.
}

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

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

--
Ian Collins.
Jun 14 '07 #8

P: n/a
"osmium" <r1********@comcast.netwrote in message
news:5d*************@mid.individual.net...
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.
Jun 14 '07 #9

P: n/a
"Thomas J. Gritzan" <Ph*************@gmx.dewrote in message
news:f4**********@newsreader3.netcologne.de...
C2064? What error message?
Oh, google says this:
http://msdn2.microsoft.com/en-us/lib...fd(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[])
Thank you. That helps.

Bruce.
Jun 14 '07 #10

P: n/a
"Ian Collins" <ia******@hotmail.comwrote in message
news:5d*************@mid.individual.net...
>void CTest::function1( )
{
char value;
void ( CTest::*pFunction )( char* param );

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

(this->*pFunction)( &value );
That's it! I had tried everything except "this->*". That was the secret.

Thank you very much!
Bruce.
Jun 14 '07 #11

P: n/a
On Jun 14, 2:56 am, "Bruce." <n...@nowhere.comwrote:
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

Jun 14 '07 #12

P: n/a
On Jun 14, 12:44 pm, terminator <farid.mehr...@gmail.comwrote:

sorry ,dont do this:
ct.*pFunction( value )
but do this one:

(ct.*pFunction)( value )

regards,
FM.

Jun 14 '07 #13

P: n/a
terminator wrote:
On Jun 14, 2:56 am, "Bruce." <n...@nowhere.comwrote:
>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
Jun 14 '07 #14

P: n/a
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?


Jun 22 '07 #15

P: n/a
Asm23 wrote:
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?

--
Please do NOT use robinton.demon.co.uk addresses
They cease to be valid on July 14
replace with Francis.Glassborow at btinternet.com
Jun 22 '07 #16

P: n/a
Francis Glassborow said:
Asm23 wrote:
<snip>
>void main()

Int main() please
Better still, int main()

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 22 '07 #17

P: n/a
On 6 23 , 1 03 , Francis Glassborow
<francis.glassbo...@btinternet.comwrote:
Asm23 wrote:
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?

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

>
--
Please do NOT use robinton.demon.co.uk addresses
They cease to be valid on July 14
replace with Francis.Glassborow at btinternet.com- -

- -

Jun 25 '07 #18

P: n/a
Asm23 wrote:
On 6 23 , 1 03 , Francis Glassborow
<francis.glassbo...@btinternet.comwrote:
>Asm23 wrote:
>>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?


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 25 '07 #19

P: n/a
"Asm23" wrote:
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();
}


Jun 25 '07 #20

P: n/a
Thanks all your replys.

espcially,
Victor Bazarov, osmium, Bruce
the link:
http://www.parashift.com/c++-faq-lit...o-members.html
is a good place to clarify my mind.

Jun 25 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.