Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointer to function and templates

Pointer to function and templates

Scheduled Pinned Locked Moved C / C++ / MFC
helpwpftutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vmaltsev
    wrote on last edited by
    #1

    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 class class 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? :^)

    P S 2 Replies Last reply
    0
    • V vmaltsev

      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 class class 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? :^)

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      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

      V 1 Reply Last reply
      0
      • P prasad_som

        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

        V Offline
        V Offline
        vmaltsev
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • V vmaltsev

          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.

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          ya you are right, lets wait for some expert for some help

          1 Reply Last reply
          0
          • V vmaltsev

            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 class class 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? :^)

            S Offline
            S Offline
            Steen Krogsgaard
            wrote on last edited by
            #5

            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 as obj.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"

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups