简体   繁体   English

C ++ - 错误:没有匹配函数来调用'Motor :: Motor()'

[英]C++ - error: no matching function for call to 'Motor::Motor()'

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()' MotorManager.cpp:7:错误:没有匹配函数来调用'Motor :: Motor()'

Second error : 第二个错误:

MotorManager.cpp:10: error: use of deleted function 'Motor& Motor::operator=(Motor&&)' MotorManager.cpp:10:错误:使用已删除的功能'Motor&Motor :: operator =(Motor &&)'

MotorManager.cpp 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 MotorManager.h

#include "Pins.h"
#include "Direction.h"
#include "Motor.h"

class MotorManager {
public:

  // Constuctor
  MotorManager();
};

Motor.cpp 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 Motor.h

#include "Direction.h"

class Motor {
public:

    // Constructor
    Motor(const int pinForwad, const int pinBackward);
private:
//Variables
int _pinForwad;
int _pinBackward;
};

Thanks 谢谢

When an instance of MotorManager is constructed, the compiler needs to initialize all of its member variables as well. 构造MotorManager的实例时,编译器也需要初始化其所有成员变量。 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. 由于Motor没有默认构造函数,因此会出现错误。

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)
{
}

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. 主要问题是,您需要在构造函数定义中使用成员初始化列表,否则MotorManager希望使用默认构造函数初始化它们。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM