微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 为什么我的Arduino类构造函数需要参数?

我正在编写一个非常简单的Arduino类来控制两个电机.

我的头文件Motor.h中有一个简单的类定义

class Motor 
{
  public:
    Motor();
    void left(int speed);
    void right(int speed);
    void setupRight(int rightSpeed_pin,int rightDirection_pin);
    void setupLeft(int leftSpeed_pin,int leftDirection_pin);
  private:
    int _rightMotorSpeedPin;
    int _rightMotorDirectionPin;
    int _leftMotorSpeedPin;
    int _leftMotorDirectionPin;
};

在我的主库文件Motor.cpp中,我有以下类构造函数

Motor::Motor() {
  // Intentionally do nothing.
}

当我尝试使用以下行在我的主程序中初始化我的类时:

Motor motor();

我收到以下编译错误

MotorClassExample.ino: In function 'void setup()':
MotorClassExample:7: error: request for member 'setupRight' in 'motor',which is of non-class type 'Motor()'
MotorClassExample:8: error: request for member 'setupLeft' in 'motor',which is of non-class type 'Motor()'
request for member 'setupRight' in 'motor',which is of non-class type 'Motor()'

令人困惑的部分是,如果我甚至包括垃圾,抛出的参数到Motor Class构造函数,如下所示:

class Motor
{
   public:
      Motor(int garbage);
      ...

在.cpp文件中:

Motor::Motor(int garbage) { }

在我的主文件中:

Motor motor(1);

一切都完美无瑕.我已经通过Arduino论坛进行了相当多的搜索,但没有找到解释这种奇怪行为的内容.为什么类构造函数需要参数?这是一些奇怪的遗物绑在AVR或其他什么?

解决方法

你遇到了“最令人烦恼的解析”问题(当然,因为它很烦人).

电动机();声明一个名为motor的函数,它不带参数并返回一个Motor. (与int test()没有区别;或类似)

要定义一个称为电机的电机实例,请使用电机电机;没有括号.

原文地址:https://www.jb51.cc/c/115037.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐