我有一个 MotorDefinition 类和一个名为 Motor 的抽象类:
class MotorDefinition {
public:
MotorDefinition(int p1, int p2, int p3) : pin1(p1), pin2(p2), pin3(p3) {}
int pin1 = -1;
int pin2 = -1;
int pin3 = -1;
};
class Motor {
public:
Motor(MotorDefinition d) : definition(d) {}
virtual void forward(int speed) const = 0;
virtual void backward(int speed) const = 0;
virtual void stop() const = 0;
protected:
MotorDefinition definition;
};
我的 Zumo 车辆有两个电机:
class MotorTypeZumoLeft : public Motor {
MotorTypeZumoLeft(MotorDefinition def) : Motor(def) {}
void Motor::forward(int speed) const {}
void Motor::backward(int speed) const {}
void Motor::stop() const {}
};
class MotorTypeZumoRight : public Motor {
MotorTypeZumoRight(MotorDefinition def) : Motor(def) {}
void Motor::forward(int speed) const {}
void Motor::backward(int speed) const {}
void Motor::stop() const {};
};
class MotorTypeZumo {
public:
MotorTypeZumo(MotorTypeZumoLeft *l, MotorTypeZumoRight *r) : left(l), right(r) {}
protected:
MotorTypeZumoLeft *left;
MotorTypeZumoRight *right;
};
不幸的是(对我来说),这不能编译:
MotorDefinition lmd(1, 2, 3);
MotorTypeZumoLeft *leftMotor(lmd);
MotorDefinition rmd(4, 5, 6);
MotorTypeZumoRight *rightMotor(rmd);
MotorTypeZumo motors(*leftMotor, *rightMotor);
我想我遗漏了一些基本概念,而且我肯定弄乱了一些语法。你能帮我正确定义这个吗,拜托。