类中的全局变量问题

时间:2017-12-16 01:26:48

标签: c++ arduino

使用以下代码,它“正常”工作。这意味着它创建了WalkerJoint的脚实例。

#include <WalkerJoint.h>

class WalkerLeg { 
  //WalkerJoint foot;
  // if this is uncommented and the below is reversed it fails

  public:
    WalkerLeg();
    void initLeg();
};

WalkerLeg::WalkerLeg(void) {
  WalkerJoint foot(2,3,4,5,6);
  //foot(2,3,4,5,6);
  //if uncommented with above it fails
}

void initLeg() {
  Serial.print("leg init\n");
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {}
  // wait for Serial comms to become ready
}

void loop() {
  int bom=0;
  WalkerLeg frontLeft;
  bom=0;
  while(bom++<100000){}
  exit(0);
}

但是,我需要foot是全局的,所以其他方法可以使用它。当我将其声明移动到类定义时,它会给出以下错误:

/Users/bin/Documents/Arduino/WalkerLeg.cpp/WalkerLeg.cpp.ino: In constructor 'WalkerLeg::WalkerLeg()':
WalkerLeg.cpp:14: error: no matching function for call to 'WalkerJoint::WalkerJoint()'
 WalkerLeg::WalkerLeg(void)
                          ^
/Users/bin/Documents/Arduino/WalkerLeg.cpp/WalkerLeg.cpp.ino:14:26: note: candidates are:
In file included from /Users/bin/Documents/Arduino/WalkerLeg.cpp/WalkerLeg.cpp.ino:1:0:
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:23:3: note: WalkerJoint::WalkerJoint(int)
   WalkerJoint(int); //for using analog inputs
   ^
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:23:3: note:   candidate expects 1 argument, 0 provided
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:22:3: note: WalkerJoint::WalkerJoint(int, int, int, int, int)
   WalkerJoint(int, int, int, int, int);
   ^
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:22:3: note:   candidate expects 5 arguments, 0 provided
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:17:7: note: constexpr WalkerJoint::WalkerJoint(const WalkerJoint&)
 class WalkerJoint
       ^
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:17:7: note:   candidate expects 1 argument, 0 provided
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:17:7: note: constexpr WalkerJoint::WalkerJoint(WalkerJoint&&)
/Users/bin/Documents/Arduino/libraries/WalkerJoint.cpp/WalkerJoint.h:17:7: note:   candidate expects 1 argument, 0 provided
WalkerLeg.cpp:18: error: no match for call to '(WalkerJoint) (int, int, int, int, int)'
     foot(2,3,4,5,6);
                   ^
exit status 1
no matching function for call to 'WalkerJoint::WalkerJoint()'

0 个答案:

没有答案