Passing a function pointer to a templated type

This is a discussion on Passing a function pointer to a templated type within the C++ Programming forums, part of the General Programming Boards category; I'm having a little trouble with my templated linked list. It has a pubic method that takes in a function ...

  1. #1
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594

    Passing a function pointer to a templated type

    I'm having a little trouble with my templated linked list. It has a pubic method that takes in a function pointer as an argument, and passes each element of the list to the function like so:

    Code:
    template <typename Type>
    class CLList
    {
    public:
         void Iterate( void (*pf)(Type &) );
    };
    
    template <typename Type>
    void CLList<Type>::Iterate( void (*pf)(Type &) )
    {
        tNode * cur = m_Head;
        while(cur)
        {
              pf(cur->data);
              cur = cur->next;
        }
    }
    Some of you have probably seen this done before. Another class of mine has a CLList as a private member, and a void function like so

    Code:
    class CMngr
    {
    private:
         CLList m_list<CObject *>;
         void UpdateObj(CObject * obj);
    }
    
    void CMngr::UpdateObj(CObject * obj)
    {
         obj->Update();
    }
    In another function of my CMngr class, I have this line:
    Code:
    m_list.Iterate(UpdateObj);
    and my error looks like so:
    Code:
    cannot convert parameter 1 from 'void (CObject *)' to 'void (__cdecl *)(Type & ) with [ Type=CObject * ]
    My first notion was that it does not like taking the reference of a pointer for some reason, and had my Iterate method take a function pointer that passes the argument by value. That did not work either.

    My next step is to make an iterator class, but I would like to see if anyone knows why my original idea did not work, and possibly if they know how to make it work.

    Thank you all for your time.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    There are two problems.
    First, UpdateObj() is a member function of CMngr that returns void and takes a single CObject* parameter. The type of "pf" is a regular C function (in other words, not a member function of CMngr).
    The second problem is that "pf" takes as a parameter a reference to Type. In this case, Type is CObject*, so a reference to that type would be CObject*&.

    Here's a simple example that compiles using your CLList class:
    Code:
    void foo(int *&p) 
    { 
    }//foo
    
    int main()
    {
        CLList<int*> ll;
    
        ll.Iterate(foo);
    
        return 0;
    }//main
    Read all about function pointers (and pointers to class member functions) here:
    http://www.function-pointer.org/

    gg

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I had a hunch that it wanted a pointer reference, but it did not fix it by changing the Iterate function to this

    Code:
     void Iterate( void (*pf)(Type ) );
    That told me something else was up, which you POINTED out (programming humor, shut up if you don't find it funny).

    Hats off to you CodePlug

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Replies: 1
    Last Post: 05-28-2002, 11:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21