|
-
Mar 10th, 2002, 07:44 PM
#1
Thread Starter
Fanatic Member
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.
-
Mar 11th, 2002, 10:15 AM
#2
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.
-
Mar 12th, 2002, 04:43 PM
#3
Thread Starter
Fanatic Member
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.
-
Mar 13th, 2002, 08:53 AM
#4
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.
-
Mar 13th, 2002, 09:40 AM
#5
Thread Starter
Fanatic Member
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.
-
Mar 14th, 2002, 03:14 PM
#6
Thread Starter
Fanatic Member
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.
-
Mar 15th, 2002, 08:23 AM
#7
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.
-
Mar 15th, 2002, 08:19 PM
#8
Thread Starter
Fanatic Member
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.
-
Mar 17th, 2002, 10:31 AM
#9
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.
-
Mar 17th, 2002, 12:02 PM
#10
Thread Starter
Fanatic Member
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.
-
Mar 18th, 2002, 01:41 PM
#11
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.
-
Mar 19th, 2002, 02:56 PM
#12
Thread Starter
Fanatic Member
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.
-
Mar 20th, 2002, 07:19 AM
#13
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.
-
Mar 20th, 2002, 04:01 PM
#14
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|