pointers to member functions with vs .net 2003

Started by
7 comments, last by warp X 18 years, 10 months ago
I tried the following code with vs .net 2003:

class Foo 
{
public: 
	double One( long inVal ); 
	double Two( long inVal ); 
};
void main() 
{
	double (Foo::*funcPtr)( long ) = &Foo::One;  
	Foo aFoo;
	double result =(aFoo.*funcPtr)( 2 );  
}





but I got this kind of compiler error: Test error LNK2019: unresolved external symbol "public: double __thiscall Foo::One(long)" (?One@Foo@@QAENJ@Z) referenced in function _main I was trying to memorize how to use these pointers. The example was from http://www.goingware.com/tips/memberpointers.html Any ideas, why it won't compile?
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
Advertisement
The function Foo::One isn't defined.
double One( long inVal ){return 0.0};
Heheh, your code is perfectly fine, that's a linker error, because you haven't defined the function. Which is A Bad Thing because you're calling it in your code [grin]
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
woops, of course! Thank you.
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
New question:

How do I use them with pointers to objects then? If I change the code like this:

class Foo {public: 	double One( long inVal ) {return 0.0;}	double Two( long inVal ) {return 0.0;}};void main() {	double (Foo::*funcPtr)( long ) = &Foo::One;  	Foo * aFoo = new Foo();	double result = aFoo->funcPtr( 2 );  }


I get this kind of error:

error C2039: 'funcPtr' : is not a member of 'Foo'
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
You forgot to dereference the function pointer.
aFoo->*funcPtr(2);
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
That is right. But, I tried it:

class Foo {public: 	double One( long inVal ) {return 0.0;}	double Two( long inVal ) {return 0.0;}};void main() {	double (Foo::*funcPtr)( long ) = &Foo::One;  	Foo * aFoo = new Foo();	double result = aFoo->*funcPtr( 2 );  }


and now it complains this:

error C2064: term does not evaluate to a function taking 1 arguments

What about this? It seems irrational.
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
double result =aFoo->*funcPtr( 2 );// todouble result =(aFoo->*funcPtr)( 2 );

That should work.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
It does. Thank you very much.
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."

This topic is closed to new replies.

Advertisement