Pages: [1]   Go Down
Author Topic: Passing Methods/Functions in Parameter  (Read 324 times)
0 Members and 1 Guest are viewing this topic.
Reading, PA
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm clearly doing something wrong here, but it all looks correct to me. Any ideas? For the most part I've removed everything except what is causing the error. It's a class that handles menu navigation on an LCD.

Code:
class MenuItem
{
public:
  typedef void (*function)();

  MenuItem *subMenus;
  function action;
  function elevate;

  MenuItem()
  {
    subMenus = NULL;
    action = NULL;
    elevate = NULL;
  }
  void elevateToSelf()
  {
    //do stuff here.
  }
  void call(function menuItemElevate)
  {
    elevate = menuItemElevate;

    if(action != NULL)
    {
      action();
    }
    else if(subMenus != NULL)
    {
      subMenus[0].call(elevateToSelf);
    }
  }
};

Code:
In file included from MenuItem.cpp:4:
/test.h: In member function 'void MenuItem::call(void (*)())':
test.h:29: error: no matching function for call to 'MenuItem::call(<unresolved overloaded function type>)'
/test.h:20: note: candidates are: void MenuItem::call(void (*)())

Thanks,
Stauffski
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6908
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Post complete code.

You're sure you don't need "*" on this line "action();"?
Logged


Reading, PA
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I'm positive. Here's an even more simplified version. Sorry for any confusion.

Code:
class MenuItem
{
public:
  typedef void (*function)();

  MenuItem *subMenus;
  function action;
  function elevate;

  MenuItem()
  {
    subMenus = NULL;
    action = NULL;
    elevate = NULL;
  }
  void elevateToSelf()
  {
    //do stuff here.
  }
  void setElevation(function menuItemElevate)
  {
    elevate = menuItemElevate;
  }
  void test()
  {
    subMenus[0].setElevation(elevateToSelf);
  }
};

Code:
In file included from MenuItem.cpp:4:
/test.h: In member function 'void MenuItem::test()':
test.h:25: error: no matching function for call to 'MenuItem::setElevation(<unresolved overloaded function type>)'
/test.h:20: note: candidates are: void MenuItem::setElevation(void (*)())
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are declaring 'function' as a regular pointer-to-function type, but you are trying to pass it a pointer-to-member-function instead. Google for "pointer to member" and you will find some explanations, e.g. http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr034.htm.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: