我正在编写一个非常简单的Arduino类来控制两个电机.
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::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或其他什么?
解决方法
原文地址:https://www.jb51.cc/c/115037.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。