我可以使用一个类来控制 C++ 中另一个类的对象吗? (阿杜诺)
Posted
技术标签:
【中文标题】我可以使用一个类来控制 C++ 中另一个类的对象吗? (阿杜诺)【英文标题】:Can I use a class to control objects from another class in C++? (Arduino) 【发布时间】:2020-06-28 02:06:57 【问题描述】:我正在尝试使用 Arduino 来控制一个带有 3 个电机的机器人底座。我有一个类来控制每个类的个体性,称为 SimpleMotor,并希望有一个类来控制称为 Driver 的 3 个 SimpleMotor。
我希望能够初始化 3 个 SimpleMotors,然后用电机对象初始化一个 Driver 对象。以下是我的代码和错误消息: 代码: 类 SimpleMotor 上市: int 电机速度 = 127; //值从 0-255 int hallPin; int enablePin; int in1Pin; int in2Pin;
SimpleMotor::SimpleMotor(int ePin, int d1Pin, int d2Pin)
enablePin = ePin;
in1Pin = d1Pin;
in2Pin = d2Pin;
void SimpleMotor::Direction(bool dir) //true is cw, false is ccw
if (dir)
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
else
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
void SimpleMotor::WriteSpeed(int motorPower)
if(motorPower < 0) Direction(0);
else Direction(1);
analogWrite(enablePin, motorPower);
void SimpleMotor::Stop()
analogWrite(enablePin, 0);
;
class Driver
friend class SimpleMotor;
public:
SimpleMotor m1;
SimpleMotor m2;
SimpleMotor m3;
Driver::Driver(SimpleMotor motor1, SimpleMotor motor2, SimpleMotor motor3)
m1 = motor1;
m2 = motor2;
m3 = motor3;
void Driver::Spin(int stickX)
if(stickX<0)
m1.Direction(0);
m2.Direction(0);
m3.Direction(0);
else
m1.Direction(1);
m2.Direction(1);
m3.Direction(1);
m1.WriteSpeed((int) floor(stickX/4));
m2.WriteSpeed((int) floor(stickX/4));
m3.WriteSpeed((int) floor(stickX/4));
;
错误:
HolonomicDriver:46:80: error: no matching function for call to 'SimpleMotor::SimpleMotor()'
Driver::Driver(SimpleMotor motor1, SimpleMotor motor2, SimpleMotor motor3)
^
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate: SimpleMotor::SimpleMotor(int, int, int)
SimpleMotor::SimpleMotor(int ePin, int d1Pin, int d2Pin)
^~~~~~~~~~~
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate expects 3 arguments, 0 provided
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(const SimpleMotor&)
class SimpleMotor
^~~~~~~~~~~
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate expects 1 argument, 0 provided
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(SimpleMotor&&)
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate expects 1 argument, 0 provided
HolonomicDriver:46:80: error: no matching function for call to 'SimpleMotor::SimpleMotor()'
Driver::Driver(SimpleMotor motor1, SimpleMotor motor2, SimpleMotor motor3)
^
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate: SimpleMotor::SimpleMotor(int, int, int)
SimpleMotor::SimpleMotor(int ePin, int d1Pin, int d2Pin)
^~~~~~~~~~~
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate expects 3 arguments, 0 provided
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(const SimpleMotor&)
class SimpleMotor
^~~~~~~~~~~
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate expects 1 argument, 0 provided
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(SimpleMotor&&)
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate expects 1 argument, 0 provided
HolonomicDriver:46:80: error: no matching function for call to 'SimpleMotor::SimpleMotor()'
Driver::Driver(SimpleMotor motor1, SimpleMotor motor2, SimpleMotor motor3)
^
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate: SimpleMotor::SimpleMotor(int, int, int)
SimpleMotor::SimpleMotor(int ePin, int d1Pin, int d2Pin)
^~~~~~~~~~~
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:10:5: note: candidate expects 3 arguments, 0 provided
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(const SimpleMotor&)
class SimpleMotor
^~~~~~~~~~~
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate expects 1 argument, 0 provided
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate: constexpr SimpleMotor::SimpleMotor(SimpleMotor&&)
c:\Users\mmell\Documents\Arduino\HolonomicDriver\HolonomicDriver.ino:2:7: note: candidate expects 1 argument, 0 provided
exit status 1
谢谢!
【问题讨论】:
简短的回答是肯定的。您遇到了不同的问题,可能只是语法错误。例如,第一个错误告诉您 SimpleMotor 没有默认构造函数。事实上,这就是所有三个错误的含义。在实现构造函数时使用初始化部分。 如果SimpleMotor的方法是public的,这里好像也不需要使用friend
了。
【参考方案1】:
正如@sweenish 已经提到的,您需要修复与构造函数相关的错误。你有两种方法:
1.添加默认构造函数:
SimpleMotor::SimpleMotor() = default;
这是一个较短的等价于
SimpleMotor::SimpleMotor() : enablePin0, in1Pin0, in2Pin0
这种方法有一个缺点是冗余构造+分配,每个电机也会有2个副本,每个可以控制你的机器人,一个在main函数中,一个在你的Driver对象中:
// in main
SimpleMotor motor1(1,2,3); // can control your motor
Driver driver(motor1, ...);
// in Driver class
SimpleMotor m1; // here we create m10, 0, 0 with default constructor
// in Driver constructor
m1 = motor1; // assign motor1 to m1. m1 can also control your motor
2.在Driver中使用指针(或引用):
class Driver
public:
std::shared_ptr<SimpleMotor> m1;
std::shared_ptr<SimpleMotor> m2;
std::shared_ptr<SimpleMotor> m3;
Driver::Driver(std::shared_ptr<SimpleMotor> motor1, std::shared_ptr<SimpleMotor> motor2, std::shared_ptr<SimpleMotor> motor3)
m1 = motor1;
m2 = motor2;
m3 = motor3;
;
那么您还需要更改创建 SimpleMotors 的方式,例如:
auto m1 = std::make_shared<SimpleMotor>(1, 2, 3);
auto m2 = std::make_shared<SimpleMotor>(5, 6, 7);
auto m3 = std::make_shared<SimpleMotor>(4, 8, 9);
Driver d(m1, m2, m3);
当然,您可以使用原始指针或 uniques_ptr 代替。您可以在此处找到有关 shared_ptr 的更多信息:https://en.cppreference.com/w/cpp/memory/shared_ptr
【讨论】:
【参考方案2】:感谢大家的帮助!
添加:
SimpleMotor::SimpleMotor() = default;
在我的 SimpleMotor 课程中确实有效。
非常感谢所有帮助!
【讨论】:
以上是关于我可以使用一个类来控制 C++ 中另一个类的对象吗? (阿杜诺)的主要内容,如果未能解决你的问题,请参考以下文章