在Arduino的构造物体组合物

问题描述 投票:0回答:2

我有我的个人步进电机一类电机。现在,我编写一个汽车类同步的步进运动,但我有问题的实例里面汽车电机类。

我得到这个错误:

Robot:238:5: error: '((Motors*)this)->Motors::a' does not have class type
 a.enable(1); b.enable(1); c.enable(1);

到目前为止,这是我的汽车类代码:

class Motors{
public:
  Motor a(xstep, xdir, xenable, xend, true);
  Motor b(ystep, ydir, yenable, yend);
  Motor c(zstep, zdir, zenable, zend);

  Motors(){

  }

  void go_home(){
    a.enable(1); b.enable(1); c.enable(1);
    a.set_dir(0); b.set_dir(0); c.set_dir(0);
    delay(mtime) 
  }
};

也尝试了汽车构造函数中实例化,但它不工作。

c++ arduino
2个回答
1
投票

我认为enableset_dir都在汽车类的方法。所以,你需要在汽车类电机的三个实例。如果你的罚款为每个汽车对象的默认构造函数,那么你可以封装他们喜欢的:

class Motors
{
  public:
    Motors() {
    }

    void go_home() {
      a.enable(1); b.enable(1); c.enable(1);
      a.set_dir(0); b.set_dir(0); c.set_dir(0);
      delay(mtime);
    }

  private:
    Motor a;
    Motor b;
    Motor c;
};

0
投票
class Motors{
public:
  Motor a = Motor(xstep, xdir, xenable, xend, amax, true);
  Motor b = Motor(ystep, ydir, yenable, yend, bmax);
  Motor c = Motor(zstep, zdir, zenable, zend, cmax);

  Motors(){

  }

  void go_home(){
    a.enable(1); b.enable(1); c.enable(1);
    a.set_dir(0); b.set_dir(0); c.set_dir(0);
    delay(mtime) 
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.