关于混淆功能间pretation [英] Confusing regarding function interpretation

查看:301
本文介绍了关于混淆功能间pretation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图接口与TFT显示屏,一个4x4键盘。我想实现一个键盘事件侦听器,使得相应的屏幕将每一次关键是pressed加载。该事件侦听器将保持跟踪它的屏幕是当前加载的,并会被监听特定键输入。

键盘事件监听器被定义(并会在设置()函数在我的主Arduino的草图被调用(我使用爱特梅尔工作室+视觉muicro Arduino的插件。在code是用C ++)

 无效Keypad_apiClass :: createKeypadEventHandler(无效)
{
    keypad44.addEventListener(screenState_keyHandler);
}

以及 screenState_keyHandler 是这样定义:

 无效screenState_keyHandler(KeypadEvent键pressed)
{
   开关(Display_api.screenDisplayID)
   {
      案件的0x0A://菜单画面
      开关(键pressed)
      {
     情况1':
     //去传感器读数屏幕
     打破;     案2:
     //进入系统设置屏幕
     打破;     案件 '#':
     //去systemReboot屏幕
     打破;     默认:
     //没做什么
     打破;
  }
  打破;  默认:
  打破;
  }
}

当我看函数的原型的addEventListener (在声明它的头文件,我看到:

 无效的addEventListener(无效(*监听器)(CHAR));

我无法理解原型的参数。难道是期待一个函数指针还是什么?可能有人请解释在原型上面的参数?

当我编译,我得到

 编译'adutoSec​​uritySystem'的'超级Arduino的W / ATmega2560(兆2560)
keypad_api.cpp:在成员函数'无效Keypad_apiClass :: createKeypadEventHandler()
keypad_api.cpp:112:50:错误:调用没有匹配的函数键盘::的addEventListener(小于未解决重载函数类型>)
keypad_api.cpp:候选人
Keypad.h:的addEventListener(无效(*)(字符))
Keypad.h:从参数1没有已知的转换'<未解决重载函数类型>'为无效(*)(字符)
编译错误

这就是为什么我很困惑!该函数存在的唯一地方是在类的成员声明:

 类Keypad_apiClass
{
 保护:
 上市:
   无效的init();
   焦炭retrieveKey pressed(无效);
   焦炭waitAndRetrieveKey pressed(无效);
   uint8_t有returnKey pressed_IntVersion(字符键pressed);
   无效createKeypadventHandler(无效);
   无效screenState_keyHandler(KeypadEvent键pressed);
};


解决方案

是的,它是一个函数指针

 无效(*监听器)(炭)
^返回类型^函数名^参数类型

所以这是一个命名函数监听器,需要一个字符参数,也没有返回值。

screenState_keyHandler 符合这个签名。

 无效screenState_keyHandler(KeypadEvent键pressed)

它没有回报,我假设 KeypadEvent 是一个typedef定义字符

I'm trying to interface a 4x4 keypad with a TFT display. I want to implement a keypad event listener such that the appropriate screen will be loaded each time a key is pressed. The event listener will be keeping track of which screen is currently loaded, and will be listening for specific key inputs.

The keypad event listener is defined (and will be called in the setup() function in my main arduino sketch (I'm using Atmel Studio + Visual muicro arduino plug-in. The code is written in C++):

void Keypad_apiClass::createKeypadEventHandler(void)
{
    keypad44.addEventListener(screenState_keyHandler);
}

And the screenState_keyHandler is defined thus:

void screenState_keyHandler(KeypadEvent keyPressed)
{
   switch(Display_api.screenDisplayID)
   {
      case 0x0A:      //menu screen
      switch(keyPressed)
      {
     case '1':
     //go to sensor readings screen
     break;

     case '2':
     //go to  system settings screen
     break;

     case '#':
     //go to  systemReboot screen
     break;

     default:
     //do nothing
     break;
  }
  break;

  default:
  break;
  }
}

When I look at the prototype of the function addEventListener (in the header file where it is declared, I see this:

void addEventListener(void (*listener)(char));

I'm having trouble understanding the parameter of the prototype. Is it expecting a function pointer or what? Could someone please explain the parameter in the prototype above?

When I compile, I get

    Compiling 'adutoSecuritySystem' for 'Arduino Mega w/ ATmega2560 (Mega 2560)'
keypad_api.cpp:In member function 'void Keypad_apiClass::createKeypadEventHandler()'
keypad_api.cpp:112:50: error: no matching function for call to 'Keypad::addEventListener(<unresolved overloaded function type>)'
keypad_api.cpp:candidate is
Keypad.h:addEventListener(void (*)(char))
Keypad.h:no known conversion for argument 1 from '<unresolved overloaded function type>' to 'void (*)(char)'
Error compiling

That's why I'm confused! The only other place the function exists is in the member declaration of the class:

class Keypad_apiClass
{
 protected:


 public:
   void init();
   char retrieveKeyPressed(void);
   char waitAndRetrieveKeyPressed(void);
   uint8_t returnKeyPressed_IntVersion(char keyPressed);
   void createKeypadventHandler(void);
   void screenState_keyHandler(KeypadEvent keyPressed);
};

解决方案

Yes it is a function pointer

void           (*listener)       (char)
^return type     ^function name   ^argument type

So it is a function named listener that takes a char argument and has no return value.

Your screenState_keyHandler fits this signature.

void screenState_keyHandler(KeypadEvent keyPressed)

It has no return, and I'm assuming that KeypadEvent is a typedef'd char

这篇关于关于混淆功能间pretation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆