I am writing a very simple Arduino class for controlling two motors.
I have the simplest class definition inside my Motor.h header file:
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; };
In my main library file, Motor.cpp, I have the following class constructor:
Motor::Motor() { // Intentionally do nothing. }
When I try to initialize my class in my main program using the line:
Motor motor();
I get the following compilation error:
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()'
The incomprehensible part is that if I even include garbage by dropping an argument into the Motor Class constructor as follows:
class Motor { public: Motor(int garbage); ...
And in the .cpp file:
Motor::Motor(int garbage) { }
And in my main file:
Motor motor(1);
Everything works fine without complaint. I searched quite a bit on the Arduino forums, but did not find anything to explain this strange behavior. Why does the class constructor need an argument? Is this some kind of weird relic related to AVR or something like that?
source share