为什么我的 Arduino Class 构造函数需要参数?
Why does my Arduino Class Constructor require an argument?
我正在写一个非常简单的 Arduino class 来控制两个电机。
我的头文件中有一个简单的 class 定义 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 中,我有以下 Class 构造函数:
Motor::Motor() {
// Intentionally do nothing.
}
当我尝试在主程序中使用以下行初始化 class 时:
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 论坛上做了很多搜索,但没有找到任何可以解释这种奇怪行为的东西。为什么 class 构造函数需要参数?这是与 AVR 相关的一些奇怪的遗物吗?
您 运行 遇到了 "most vexing parse" 问题(之所以这样命名,当然是因为它很烦人)。
Motor motor();
声明了一个名为 motor
的 函数 ,它不带任何参数,并且 returns 是一个 Motor
。 (与 int test();
或类似的没有区别)
要定义一个名为 motor
的 Motor
实例,请使用不带括号的 Motor motor;
。
作为已接受答案的补充:
从 C++11 开始,您可以使用 "uniform initialization" 语法。统一初始化优于函数形式的一个优点是,大括号不能与函数声明混淆,这在您的情况下发生了。
语法可以像函数式一样使用,除了你使用大括号 {}.
有一些很好的例子over here
Rectangle rectb; // default constructor called
Rectangle rectc(); // function declaration (default constructor NOT called)
Rectangle rectd{}; // default constructor called
我正在写一个非常简单的 Arduino class 来控制两个电机。
我的头文件中有一个简单的 class 定义 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 中,我有以下 Class 构造函数:
Motor::Motor() {
// Intentionally do nothing.
}
当我尝试在主程序中使用以下行初始化 class 时:
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 论坛上做了很多搜索,但没有找到任何可以解释这种奇怪行为的东西。为什么 class 构造函数需要参数?这是与 AVR 相关的一些奇怪的遗物吗?
您 运行 遇到了 "most vexing parse" 问题(之所以这样命名,当然是因为它很烦人)。
Motor motor();
声明了一个名为 motor
的 函数 ,它不带任何参数,并且 returns 是一个 Motor
。 (与 int test();
或类似的没有区别)
要定义一个名为 motor
的 Motor
实例,请使用不带括号的 Motor motor;
。
作为已接受答案的补充:
从 C++11 开始,您可以使用 "uniform initialization" 语法。统一初始化优于函数形式的一个优点是,大括号不能与函数声明混淆,这在您的情况下发生了。 语法可以像函数式一样使用,除了你使用大括号 {}.
有一些很好的例子over here
Rectangle rectb; // default constructor called
Rectangle rectc(); // function declaration (default constructor NOT called)
Rectangle rectd{}; // default constructor called