Reputation: 25
i have a problem with my code.
I have two errors :
Fist error :
MotorManager.cpp:7: error: no matching function for call to 'Motor::Motor()'
Second error :
MotorManager.cpp:10: error: use of deleted function 'Motor& Motor::operator=(Motor&&)'
MotorManager.cpp
#include "MotorManager.h"
#include "Pins.h"
#include "Direction.h"
#include "Motor.h"
MotorManager::MotorManager() {
// Init motors
_motorLeftFront = Motor(MOTOR_FRONT_LEFT_FORWARD, MOTOR_FRONT_LEFT_BACKWARD);
_motorRightFront = Motor(MOTOR_FRONT_RIGHT_FORWARD, MOTOR_FRONT_RIGHT_BACKWARD);
_motorLeftBack = Motor(MOTOR_BACK_LEFT_FORWARD, MOTOR_BACK_LEFT_BACKWARD);
_motorRightBack = Motor(MOTOR_BACK_RIGHT_FORWARD, MOTOR_BACK_RIGHT_BACKWARD);
}
MotorManager.h
#include "Pins.h"
#include "Direction.h"
#include "Motor.h"
class MotorManager {
public:
// Constuctor
MotorManager();
};
Motor.cpp
#include "Motor.h"
#include "Direction.h"
Motor::Motor(const int pinForwad, const int pinBackward) : _pinForwad(pinForwad), _pinBackward(pinBackward) {
pinMode(pinForwad, OUTPUT);
pinMode(pinBackward, OUTPUT);
}
Motor.h
#include "Direction.h"
class Motor {
public:
// Constructor
Motor(const int pinForwad, const int pinBackward);
private:
//Variables
int _pinForwad;
int _pinBackward;
};
Thanks
Upvotes: 1
Views: 496
Reputation: 1
The primary problem is, that you'll need to use the member initializer list in the constructor definition, because otherwise MotorManager
wants to initialize them with the default constructor.
You can fix it like this:
MotorManager::MotorManager()
: _motorLeftFront(MOTOR_FRONT_LEFT_FORWARD, MOTOR_FRONT_LEFT_BACKWARD)
, _motorRightFront(MOTOR_FRONT_RIGHT_FORWARD, MOTOR_FRONT_RIGHT_BACKWARD)
, _motorLeftBack(MOTOR_BACK_LEFT_FORWARD, MOTOR_BACK_LEFT_BACKWARD)
, _motorRightBack(MOTOR_BACK_RIGHT_FORWARD, MOTOR_BACK_RIGHT_BACKWARD)
{}
MotorManager.cpp:10: error: use of deleted function 'Motor& Motor::operator=(Motor&&)'
That's a follow up error because you used assignment statements
_motorLeftFront = Motor(MOTOR_FRONT_LEFT_FORWARD, MOTOR_FRONT_LEFT_BACKWARD);
but didn't declare a move assignment operator overload like:
Motor& Motor::operator=(Motor&&) = default;
or at least copy assignment:
Motor& Motor::operator=(const Motor&) = default;
Upvotes: 1
Reputation: 30418
When an instance of MotorManager
is constructed, the compiler needs to initialize all of its member variables as well. Since the members aren't initialized in the initializer list, it attempts to call the default constructor for the class. Since Motor
doesn't have a default constructor, you get the error.
This can be fixed by initializing the member variables in the initializer list:
MotorManager::MotorManager()
: _motorLeftFront(MOTOR_FRONT_LEFT_FORWARD, MOTOR_FRONT_LEFT_BACKWARD),
_motorRightFront(MOTOR_FRONT_RIGHT_FORWARD, MOTOR_FRONT_RIGHT_BACKWARD),
_motorLeftBackMOTOR_BACK_LEFT_FORWARD, MOTOR_BACK_LEFT_BACKWARD),
_motorRightBack(MOTOR_BACK_RIGHT_FORWARD, MOTOR_BACK_RIGHT_BACKWARD)
{
}
Upvotes: 2