Pointer to function and templates
-
Greetings, everyone. I stuck with an error "C2064: term does not evaluate to a function taking 1 arguments" when I try to pass pointer to class method as an argument to some method in template. Let's say I have a template with public method 'UserFunction' and private recursive method 'UserFunctionRecursive'
template <class TxData> class CxTemplate { public: ... template <typename TxFunction> void UserFunction(TxFunction pFunction) { UserFunctionRecursive<TxFunction>(xData, pFunction); } ... private: template <typename TxFunction4Recursion> void UserFunctionRecursive(TxData xData, TxFunction4Recursion pFunction) { if(bla-bla) UserFunctionRecursive<TxFunction4Recursion>(xData, pFunction); else !!!!=> pFunction(xData); } }
and I have a class which has user function and object of that template classclass CxClass { public: ... void Foo(int i) { ... } CxTemplate<int> m_xObject; }
When I try to call 'UserFunction' method and pass Foo as an argument, I get the C2064 error (on line marked with !!!!=> above) CxClass obj; obj.m_xObject.UserFunction(CxClass::Foo); // error and it's quite clear why, there is no instance of that function. So we have to make it static and it'll work. But, static function has no access to non-static members, so I can't make it static. Any ideas how to make it work? :^) -
Greetings, everyone. I stuck with an error "C2064: term does not evaluate to a function taking 1 arguments" when I try to pass pointer to class method as an argument to some method in template. Let's say I have a template with public method 'UserFunction' and private recursive method 'UserFunctionRecursive'
template <class TxData> class CxTemplate { public: ... template <typename TxFunction> void UserFunction(TxFunction pFunction) { UserFunctionRecursive<TxFunction>(xData, pFunction); } ... private: template <typename TxFunction4Recursion> void UserFunctionRecursive(TxData xData, TxFunction4Recursion pFunction) { if(bla-bla) UserFunctionRecursive<TxFunction4Recursion>(xData, pFunction); else !!!!=> pFunction(xData); } }
and I have a class which has user function and object of that template classclass CxClass { public: ... void Foo(int i) { ... } CxTemplate<int> m_xObject; }
When I try to call 'UserFunction' method and pass Foo as an argument, I get the C2064 error (on line marked with !!!!=> above) CxClass obj; obj.m_xObject.UserFunction(CxClass::Foo); // error and it's quite clear why, there is no instance of that function. So we have to make it static and it'll work. But, static function has no access to non-static members, so I can't make it static. Any ideas how to make it work? :^)va`Lery wrote: UserFunctionRecursive(xData, pFunction); you need to call it like UserFunctionRecursive(xData, pFunction) not UserFunctionRecursive**<TxFunction>(xData, pFunction) va`Lery wrote: _if(bla-bla) UserFunctionRecursive<TxFunction4Recursion>(**xData, pFunction); else!!!!=> pFunction(xData);_ make change like above -- modified at 6:12 Thursday 29th September, 2005
-
va`Lery wrote: UserFunctionRecursive(xData, pFunction); you need to call it like UserFunctionRecursive(xData, pFunction) not UserFunctionRecursive**<TxFunction>(xData, pFunction) va`Lery wrote: _if(bla-bla) UserFunctionRecursive<TxFunction4Recursion>(**xData, pFunction); else!!!!=> pFunction(xData);_ make change like above -- modified at 6:12 Thursday 29th September, 2005
Thanks for the suggestion, but it changes nothing. The problem is that I can pass as an argument only static method or global function, and it works with the template I have. But, I need to pass pointer to method of existing instance of a class. That's where I'm lost.
-
Thanks for the suggestion, but it changes nothing. The problem is that I can pass as an argument only static method or global function, and it works with the template I have. But, I need to pass pointer to method of existing instance of a class. That's where I'm lost.
ya you are right, lets wait for some expert for some help
-
Greetings, everyone. I stuck with an error "C2064: term does not evaluate to a function taking 1 arguments" when I try to pass pointer to class method as an argument to some method in template. Let's say I have a template with public method 'UserFunction' and private recursive method 'UserFunctionRecursive'
template <class TxData> class CxTemplate { public: ... template <typename TxFunction> void UserFunction(TxFunction pFunction) { UserFunctionRecursive<TxFunction>(xData, pFunction); } ... private: template <typename TxFunction4Recursion> void UserFunctionRecursive(TxData xData, TxFunction4Recursion pFunction) { if(bla-bla) UserFunctionRecursive<TxFunction4Recursion>(xData, pFunction); else !!!!=> pFunction(xData); } }
and I have a class which has user function and object of that template classclass CxClass { public: ... void Foo(int i) { ... } CxTemplate<int> m_xObject; }
When I try to call 'UserFunction' method and pass Foo as an argument, I get the C2064 error (on line marked with !!!!=> above) CxClass obj; obj.m_xObject.UserFunction(CxClass::Foo); // error and it's quite clear why, there is no instance of that function. So we have to make it static and it'll work. But, static function has no access to non-static members, so I can't make it static. Any ideas how to make it work? :^)I'm in no way an expert on templates, but since nobody else have found a solution I'll give it a try. I don't see any place where you tell the compiler what TxFunction is to be instantiated to. To be a pointer to a CxClass member taking an int parameter returning void it should be
void (CxClass::*)(int)
so perhaps you should call UserFunction asobj.m_xObject.UserFunction<void (CxClass::*)(int)>(CxClass::Foo)
(i would make a typedef of the member function pointer type!) I haven't tried it so I have no idea if it will work. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"