Go Down

Topic: Passing Methods/Functions in Parameter (Read 448 times) previous topic - next topic

stauffski

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: [Select]

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: [Select]

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

liudr

Post complete code.

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

stauffski

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

Code: [Select]

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: [Select]

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 (*)())

dc42

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.
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.

Go Up