![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 11
| Problem: passing function as argument !!! This call is made by reference, and the function called is declared into a class. A example of my problem is: Code: class ExClass {
public:
int func(const int &); //***
};
int ExClass::func(const int &z) { return(z); }
template <class T>
void example(T &func) {}
int main(){
ExClass f;
example( f.func );
return(0);
} In function ‘int main()’: error: no matching function for call to ‘example(<unresolved overloaded function type>)’ note: candidates are: void example(T&) [with T = int (ExClass::*)(const int&)] *** It's good to mention that if I declared "func" as "static int func(const int &);" into the "ExClass", I get no error. But I need let "func" be non-static. What am I supposed to do? Thank you. |
| tharnier is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You can't get from a "plain" function pointer to a pointer to member function, or back the other way. (Since a member function has to be called from an object of the class, and has to know about that object, etc., which a "plain" function pointer can't.) What are you trying to do, really? |
| tabstop is offline | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> Hi, I'm having a problem in pass a function as argument to another function. It can't work because you're passing in a function that takes an implicit pointer to *this, which you haven't supplied, and without an object the function cannot be called, obviously. |
| Sebastiani is offline | |
| | #4 |
| Registered User Join Date: Jul 2009
Posts: 11
| I'm intending to use a 'ready' function, Broydn, from 'Numerical Recipes Book". And that function, Broydn, calls aother function by reference. For example: Code: template <class T>
void Broydn( T &func ) {}
Code: int main(){
ExClass f;
Broydn( f.func );
return(0);
}
Thank you. |
| tharnier is offline | |
| | #5 |
| Registered User Join Date: Oct 2006
Posts: 263
| I don't think you can do this without a static member. I tried a few different things on my own, and it seems you can't take the address of a non-static member function. |
| Elkvis is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Well, I had thought we had covered why: You would need an ExClass object to call your function with -- and Broydn quite simply won't have such an object, and therefore quite simply won't be able to call the function. What is this object ExClass doing for you? (I mean, I had to look up Broyden's method just now, and it appears to be a root-finder.) If you need this class, I suppose you can adjust your root-finder to take an object and a member function pointer, so that your root finder will have the object needed to call the function with. |
| tabstop is offline | |
| | #7 | |
| Registered User Join Date: Jul 2009
Posts: 11
| Quote:
Code: int main(){
ExClass f, *p=&f;
p -> func;
...
}
| |
| tharnier is offline | |
| | #8 | |
| Registered User Join Date: Jul 2009
Posts: 11
| Quote:
Broydn is a root finder, you're right. And you let my problem clear; I need that Broydn be able to get an object from my class ExClass. But how should I do that? I thought that if I had declared Broydn as "friend" of "ExClass", so Broydn could use a member function even without a object, couldn't?? Last edited by tharnier; 07-16-2009 at 12:59 PM. | |
| tharnier is offline | |
| | #9 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Two examples. One using boost, the other without boost. Code: class ExClass
{
public:
int func(const int& z); //***
};
int ExClass::func(const int& z) { return z; }
template <typename T>
void example(T& func) { func(); }
template <typename Class_t, typename Fnc_t>
void example2(Class_t& Class, Fnc_t FncPtr) { (Class.*FncPtr)(); }
int main()
{
ExClass f;
example( boost::bind(&ExClass::func, f) );
example2(f, &ExClass::func);
return 0;
}
https://apps.sourceforge.net/mediawi...arameter_names Quote:
The problem is that the functions are members OF objects. They are not global! So if you want to call a member function, you need to specify which class instance's member function you want to call! Thus you need the instance AND the member function pointer.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #10 |
| Guest Join Date: Aug 2001
Posts: 4,923
| What you're looking for is a 'functor'. In its crudest form it would work something like this: Code: class ExClass {
public:
int func(const int & value)
{
cout << "Value: " << value << endl;
return value;
}
struct wrapper
{
wrapper(ExClass* this_)
: this_(this_)
{ }
int operator ( )(const int & value)
{
return this_->func(value);
}
ExClass* this_;
};
wrapper wrap(void)
{
return wrapper(this);
}
};
template <class T>
void example(T &func)
{
int value = 10;
func(value);
}
int main(void) {
ExClass
e;
ExClass::wrapper
f = e.wrap( );
example(f);
}
Last edited by Sebastiani; 07-16-2009 at 01:51 PM. Reason: forgotten return values |
| Sebastiani is offline | |
| | #11 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| In that case, I'll throw in another, better example: Code: class ExClass
{
public:
int func(const int& z); //***
};
int ExClass::func(const int& z) { return z; }
template<typename Class_t, typename Fnc_t, typename Return_t>
class Functor
{
public:
Functor(Class_t& Class, Fnc_t FncPtr): m_Class(Class), m_FncPtr(FncPtr) { }
Return_t operator () { return m_FncPtr(); }
const Return_t operator () const { return m_FncPtr(); }
private:
Class_t m_Class;
Fnc_t m_FncPtr;
};
template<typename Return_t, typename Class_t, typename Fnc_t>
Functor<Class_t, Fnc_t, Return_t> FunctorWrapper(Class_t& Class, Fnc_t FncPtr)
{
return Functor<Class_t, Fnc_t, Return_t>(Class, FncPtr);
}
template<typename T> void example(T& Functor) { Functor(); }
int main()
{
ExClass f;
example( FunctorWrapper<int>(f, &ExClass::func) );
return 0;
}
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #12 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> I'm not sure how it handles void return type, however. AFAIK, it should handle that automagically. I could be wrong, though. >> const Return_t You probably shouldn't return const like that. The user might not want it to be, and anyway it wouldn't work if the type was already const. At any rate, yours is a much better way to handle a functor...I was just being lazy. |
| Sebastiani is offline | |
| | #13 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| "friend" just means that it can access private variables of an object which it still needs to have -- you can't still talk about "the" variable x, it's bob's variable x, or fred's variable x, or joe's variable x, et cetera. Members don't exist by themselves, only as part of other objects (hence the name). |
| tabstop is offline | |
| | #14 |
| Registered User Join Date: Jul 2009
Posts: 11
| |
| tharnier is offline | |
| | #15 | |||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
Quote:
It's critical if we pass the object through a const reference. So don't use "const" in the type and it'll work
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |||
| Elysia is offline | |
![]() |
| Tags |
| argument, function, overloaded, reference, type |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Passing A Function Into A Constructor | fourplay | C++ Programming | 6 | 03-15-2009 06:06 AM |
| Compiling sample DarkGDK Program | Phyxashun | Game Programming | 6 | 01-27-2009 03:07 AM |
| Passing function as argument | NeMewSys | C Programming | 16 | 06-11-2008 02:03 PM |
| passing a function as an argument | angelscars | C++ Programming | 6 | 12-06-2005 09:53 PM |
| Didn't quite know where to post this.......compiler problem... | incognito | C++ Programming | 5 | 02-08-2003 07:42 PM |