-
no matches converting function
Hello!
I'm newbie in C++. I want write a Palm application.
I've a class, and I can't pass
its member function to FrmSetEventHandler:
class MyClass {
public:
Boolean eventHandler(EventPtr event) {
...
}
Boolean applicationHandleEvent(EventPtr event) {
...
FrmSetEventHandler(frm, eventHandler);
...
}
}
I get this message:
no matches converting function `eventHandler' to type `Boolean
(*)(EventType *)'
How can I solve this problem?
Ben
ps:
( EventPtr = EventType* )
-
Re: no matches converting function
First of all, don't use Boolean. C++ has a built-in bool type so there's
no need to use home-made types to simulate a Boolean variable.
The problem is that you're confusing a member function with an ordinary
function. These are two entirely different creatures. You can make your
member function static (this is a quick and dirty solution to you
problem but probably a bad idea in the long run) or use real pointers to
members. See how it is done here:
http://gethelp.devx.com/techtips/cpp...0minJuly98.asp
Danny
Ben wrote:
>
> Hello!
>
> I'm newbie in C++. I want write a Palm application.
> I've a class, and I can't pass
> its member function to FrmSetEventHandler:
>
> class MyClass {
> public:
> Boolean eventHandler(EventPtr event) {
> ...
> }
>
> Boolean applicationHandleEvent(EventPtr event) {
> ...
> FrmSetEventHandler(frm, eventHandler);
> ...
> }
> }
>
> I get this message:
> no matches converting function `eventHandler' to type `Boolean
> (*)(EventType *)'
>
> How can I solve this problem?
>
> Ben
>
> ps:
> ( EventPtr = EventType* )
-
Re: no matches converting function
> First of all, don't use Boolean. C++ has a built-in bool type so there's
> no need to use home-made types to simulate a Boolean variable.
Boolean isn't my idea. It is part of PalmOS SDK.
> The problem is that you're confusing a member function with an ordinary
> function. These are two entirely different creatures. You can make your
> member function static (this is a quick and dirty solution to you
> problem but probably a bad idea in the long run) or use real pointers to
> members. See how it is done here:
> http://gethelp.devx.com/techtips/cpp...0minJuly98.asp
static member is OK.
But pointer doesn't work properly:
#include <PalmOS.h>
typedef Boolean (*HandlerType)(EventPtr);
class MyClass {
public:
Boolean eventHandler(EventPtr event) {
return true;
}
Boolean setHandler() {
FormPtr frm;
HandlerType myFunc = &MyClass::eventHandler;
FrmSetEventHandler(frm, myFunc);
return true;
}
};
m68k-palmos-gcc.exe -Wall -c MyClass.cc
MyClass.cc: In method `Boolean MyClass::setHandler()':
MyClass.cc:14: converting from `Boolean (MyClass::*)(EventType *)' to
`Boolean (*)(EventType *)'
What's the problem?
Thanks.
Ben
-
Re: no matches converting function
Ben wrote:
>
> > First of all, don't use Boolean. C++ has a built-in bool type so there's
> > no need to use home-made types to simulate a Boolean variable.
> Boolean isn't my idea. It is part of PalmOS SDK.
Yeah, I suspected that... but doesn't plain bool work?
>
> > The problem is that you're confusing a member function with an ordinary
> > function. These are two entirely different creatures. You can make your
> > member function static (this is a quick and dirty solution to you
> > problem but probably a bad idea in the long run) or use real pointers to
> > members. See how it is done here:
> > http://gethelp.devx.com/techtips/cpp...0minJuly98.asp
>
> static member is OK.
>
> But pointer doesn't work properly:
>
> #include <PalmOS.h>
>
> typedef Boolean (*HandlerType)(EventPtr);
So far so good...
>
> class MyClass {
> public:
> Boolean eventHandler(EventPtr event) {
> return true;
> }
Alas! you need to declare eventHandler as a static member function.
>
> Boolean setHandler() {
> FormPtr frm;
>
> HandlerType myFunc = &MyClass::eventHandler;
Once you declare eventHanlder static, this assignment should work.
Danny
-
Re: no matches converting function
> Alas! you need to declare eventHandler as a static member function.
Ok. My question is: How can I do it without 'static' keyword?
Is it possible?
Ben
-
Re: no matches converting function
Ben wrote:
>
> > Alas! you need to declare eventHandler as a static member function.
>
> Ok. My question is: How can I do it without 'static' keyword?
> Is it possible?
class MyClass {
public:
static Boolean eventHandler(EventPtr event) {
return true;
}
};
Danny
-
Re: no matches converting function
Danny Kalev wrote:
>
> Ben wrote:
>
>>>Alas! you need to declare eventHandler as a static member function.
>>
>> Ok. My question is: How can I do it without 'static' keyword?
>> Is it possible?
>
>
> class MyClass {
> public:
> static Boolean eventHandler(EventPtr event) {
> return true;
> }
> };
>
> Danny
Without 'static'?
--------
So: impossible?
Ben
-
Re: no matches converting function
Ben wrote:
>
> Danny Kalev wrote:
> >
> > Ben wrote:
> >
> >>>Alas! you need to declare eventHandler as a static member function.
> >>
> >> Ok. My question is: How can I do it without 'static' keyword?
> >> Is it possible?
> >
> >
> > class MyClass {
> > public:
> > static Boolean eventHandler(EventPtr event) {
> > return true;
> > }
> > };
> >
> > Danny
>
> Without 'static'?
> --------
without static this would be an ordinary member function and as such,
you'd need to use pointers to members, not pointers to functions, as I
explained before.
>
> So: impossible?
It's possible. Please read the article I referred you to. If you have
specific questions, you can post them here. However, the scope of a
newsgroup post doesn't lend itself for a full-blown tutorial on how to
use pointers to member functions.
Danny
Danny
>
> Ben
-
Re: no matches converting function
Hi!
> without static this would be an ordinary member function and as such,
> you'd need to use pointers to members, not pointers to functions, as I
> explained before.
This way is not enough for me.
> It's possible. Please read the article I referred you to. If you have
I found some articles on this subject:
http://groups.google.com/groups?hl=e....166959%2540rw
crnsc51.ops.asp.att.net%26rnum%3D10
(this link is too long)
So: there aren't solution.
> specific questions, you can post them here. However, the scope of a
> newsgroup post doesn't lend itself for a full-blown tutorial on how to
> use pointers to member functions.
Sorry. I'm very beginner.
Ben
-
Re: no matches converting function
Ben wrote:
>
> Hi!
>
> > without static this would be an ordinary member function and as such,
> > you'd need to use pointers to members, not pointers to functions, as I
> > explained before.
>
> This way is not enough for me.
>
> > It's possible. Please read the article I referred you to. If you have
>
> I found some articles on this subject:
>
> http://groups.google.com/groups?hl=e...m5.166959%2540
rwcrnsc51.ops.asp.att.net%26rnum%3D10
>
> (this link is too long)
>
> So: there aren't solution.
>
> > specific questions, you can post them here. However, the scope of a
> > newsgroup post doesn't lend itself for a full-blown tutorial on how to
> > use pointers to member functions.
>
> Sorry. I'm very beginner.
No problem. And I'm sorry if you got the wrong impression that I expect
you to be a seasoned programmer; it's fine and well not to know the
answer to a technical question. After all, that's why we are here. But
we're here to teach you how to fish, not give you a fish. As a C++
programmer, however novice as one might be, your first step should
always be to find the answers by yourself. When I answered your first
post I assumed that you knew what static member functions are (this is a
fairly common C++ concept). If you didn't know that, you should have
stated that right from the start instead of saying that "static is OK"
only to disclose two posts later that you hadn't understood what I'd
said.
Back to our discussion: pointers to member functions are a tricky and
complex issue. I have serious doubts whether you should, as a "very
beginner" programmer, try to master this concept without learning more
fundamental features of C++ first. Static members for example. I could
have given a way the solution but it would be unfair. You wouldn't have
learned a thing from it and ended up cheating yourself. That is exactly
why we refuse to solve homework assignments here. My advice is to get a
good C++ primer, master the basic features of the language, say the
built-in types, class member functions, ordinary functions, and static
member functions, then move on to more advanced topics such as
callbacks.
Does that sound agreeable or am I missing something?
BTW, you can also browse the C++ Tip of the Day archive and find many
useful tips about function pointers vs. pointers to member functions. To
be honest, I'm not sure they would be suitable for beginners but perhaps
they will shed some light on the topic:
http://www.devx.com/free/tips/tipvie...ontent_id=2336
http://www.devx.com/free/tips/tipvie...ontent_id=2343
http://www.devx.com/free/tips/tipvie...ontent_id=2344
Danny
Danny
>
> Ben
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks