Results 1 to 14 of 14

Thread: returning a pointer to a function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    returning a pointer to a function

    i have a templated class with this template header
    Code:
    template<class InputType, class ReturnType, class ParamType1, class ParamType2>
    and a variable declared as such
    Code:
    ReturnType (*m_pFunc)(ParamType1, ParamType2);
    a function prototype
    Code:
    ReturnType (*)(ParamType1, ParamType2) GetFuncPtr(void);
    and the function body defined as follows
    Code:
    template<class InputType, class ReturnType, class ParamType1, class ParamType2>
    ReturnType (*)(ParamType1, ParamType2) CMenuItem<InputType, ReturnType, ParamType1, ParamType2>::GetFuncPtr(void)
    {}
    it tells me syntax error: ')' in the function prototype
    and unexpected tokens preceding ';'
    and then some more errors that are created because of this

    so what am i doing wrong? it all looks like it should logically work?
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What compiler do you use? MSVC++6 has some problems with templates.
    Although this code should work, I see no error.
    Best thing is to use a typedef inside the class:
    Code:
    typedef ReturnType (*CLASSCALLBACK)(ParamType1, ParamType2);
    CLASSCALLBACK m_pFunc;
    CLASSCALLBACK GetFuncPtr();
    
    
    // outside:
    template<class InputType, class ReturnType, class ParamType1, class ParamType2>
    CMenuItem<InputType, ReturnType, ParamType1, ParamType2>::CLASSCALLBACK
    CMenuItem<InputType, ReturnType, ParamType1, ParamType2>::GetFuncPtr()
    {}
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    one small problem on this line in the outside file

    CMenuItem<InputType, ReturnType, ParamType1, ParamType2>::CLASSCALLBACK

    i think its treating that as the data type for the function and it won't work if i put a semicolon like this

    CMenuItem<InputType, ReturnType, ParamType1, ParamType2>::CLASSCALLBACK;
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Actually it is the return type of the function GetFuncPtr(). It is a type defined inside the class, so I need the scope resolution operator and also the template parameters.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    oh duh, i get it now
    everything works perfectly, thanks
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    well that class compiles, but when i use it it starts giving me errors

    this is where the function pointer is defined
    Code:
    template<class InputType, class ReturnType, class ParamType1, class ParamType2>
    class CMenuItem
    {
    	public:
                                    typedef ReturnType (*FUNCTION_PTR)(ParamType1, ParamType2);
                                    void SetFuncPtr(FUNCTION_PTR Func);
    
    	private:
    		FUNCTION_PTR m_pFunc;
    }
    CRobots class...
    Code:
    CRobot* SendToJob(CJob* Job, int i);
    and the function i'm attempting to call that isn't compiling right...
    Code:
    menuItem->SetFuncPtr(robots.SendToJob);
    -----------------------------------------------------------------
    error C2664: 'SetFuncPtr' : cannot convert parameter 1 from 'class CRobot *(class CJob *,int)' to 'class CRobot *(__cdecl *)(class CJob *,int)'
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Is AddToJob a member function of a class? In that case, it wouldn't work anyway, because you can't assign a member function pointer to an ordinary function pointer. You can have special member function poiners, but they are specific to one class (can maybe be solved with template parameters too). Here is a pointer to a the AddToJob function, assuming AddToJob is a member function of CRobot.
    typedef CRobot* (CRobot::*FUNC_PTR)(CJob*, int);

    to call, you need a valid CRobot object, here it is robot:
    // FUNC_PTR m_func;
    robot.*m_func(pJob, 34);

    if you have a pointer:
    pRobot->*m_func(pJob, 34);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    ok thanx

    this whole thing has just turned into one huge mess
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Maybe it's possible with templates:

    template <class _RT, class _PT1, class _PT2, class _OT>
    class x
    {
    typedef _RT (_OT::* FUNC_PTR)(_PT1, _PT2);
    };
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    thats exactly how i have it set up, and then the user passes in a pointer to a class of type _OT which is stored in _OT* m_pClass;

    then when i call it i do m_pClass->*m_pFunc(*m_pParam1, *m_pParam2);

    and it tells me that the term does not evaluate to a function
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    maybe
    m_pClass->(*m_pFunc)(*m_pParam1, *m_pParam2);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    here is a small version i created to test just to make sure i wasn't doing something wrong on my big project

    the code is exactly how i have it setup in my other project and as far as i can tell its also exactly what you've been telling me to do and it still gives the same error, and also adding ( ) create yet another compile error

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    class AnotherClass
    {
    public:
    	AnotherClass(){}
    
    	void Me(void)
    	{
    		cout << "nothing\n";
    	}
    
    private:
    
    };
    ////////////////////////////////////////////
    
    template<class _Ty>
    class Menu
    {
    public:
    	typedef void (_Ty::*FUNC)(void);
    
    	Menu(){}
    
    	void SetFunc(FUNC func)
    	{
    		m_func = func;
    	}
    
    	void CallFunc(_Ty *SomeClass)
    	{
    		SomeClass->*m_func();
    	}
    
    private:
    	FUNC m_func;
    };
    ////////////////////////////////////////////
    
    int main()
    {
    	AnotherClass b;
    	Menu<AnotherClass> m;
    	m.SetFunc(b.Me);
    	m.CallFunc(&b);
    	
    	return 0;
    }
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    m.SetFunc(b.Me);
    This line works in VC++ but should be
    m.SetFunc(AnotherClass::Me);
    for standard compliance (the first does not compile on borland turbo c++)

    This function call compiles on both compiles:
    (SomeClass->*m_func)();

    Test run gives correct result ("nothing")
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    thank you so much
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width