'.' 之前的预期不合格 ID令牌 arduino 库 [英] expected unqualified-id before '.' token arduino library

查看:43
本文介绍了'.' 之前的预期不合格 ID令牌 arduino 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

在函数void loop()"中:headers_stepper_test:12:错误:预期'.' 之前的非限定 ID'.' 之前的令牌应为非限定 ID令牌

In function 'void loop()': headers_stepper_test:12: error: expected unqualified-id before '.' token expected unqualified-id before '.' token

在这段代码中:

#include "StepperMotor.h"

void setup() {
  // put your setup code here, to run once:
StepperMotor(8,9);
}

void loop() {
  // put your main code here, to run repeatedly:


  void StepperMotor.moveDegrees(-180);
  delay(1000);

}

cpp 库文件:

#include "Arduino.h"

StepperMotor::StepperMotor(int pin1, int pin2)
{
    dirPin=pin1;
    pinMode(dirPin,OUTPUT);
      stepperPin=pin2;
      pinMode(stepperPin,OUTPUT);

}

void StepperMotor::stepDegrees(bool dir, int steps);
{
 digitalWrite(dirPin,dir);
 delay(50);
 for(int i=0;i<steps;i++){
   digitalWrite(stepperPin, HIGH);
   delayMicroseconds(800);
   digitalWrite(stepperPin, LOW);
   delayMicroseconds(800);
}

void StepperMotor::moveDegrees(int degreeNumber);
{
  if (degrees > 0){
    userAbs = (degreeNumber);
    stepNumber = (userAbs * 200/360);
    step(true,stepNumber);
 }
  if (degrees < 0){
    userAbs = (-1*degreesNumber);
    stepNumber = (userAbs * 200/360);
    step(false,stepNumber);
  }
}

.h 头文件:

#ifndef StepperMotor_h
#define StepperMotor_h

#include "Arduino.h"

class StepperMotor
{
    public:
            StepperMotor(int pin1, int pin2);
     void moveDegrees(int degreeNumber);
    private:
     void stepDegrees(bool dir, int steps);
         int dirPin;
         int stepPin;
         float userAbs;
         float stepNumber;
};
#endif

当我尝试实现我自己的库文件时.我不确定在这种情况下是否需要void",但无论如何都会出现相同的错误.这是什么原因造成的?

When I am trying to implement my own library files. I am not sure if I need the "void" in this case but the same error comes up either way. What is causing this?

推荐答案

让我们回顾一下您的代码:

Lets recap your code:

void loop() {
    // put your main code here, to run repeatedly:
    void StepperMotor.moveDegrees(-180);
    delay(1000);
}

第一件事:不要将 void 放在对 moveDegress() 的调用中.

First thing first: Don't put void in the call to moveDegress() there.

第二:

方法 moveDegrees 不是静态的,因此,您需要一个 StepperMotor 类的实例才能调用它:

The method moveDegrees is not static, therefore, you need an instance of the class StepperMotor in order to call it:

// note: you can initialize the variable here 
// but I'll  do it in setup
StepperMotor stepperMotorInstance; // your variable

void setup() {
    stepperMotorInstance = StepperMotor(8,9); // option 2: initialize variable in setup
}

void loop() {
    // put your main code here, to run repeatedly:
    stepperMotorInstance.moveDegrees(-180);
    delay(1000);
}

希望能帮到你

这篇关于'.' 之前的预期不合格 ID令牌 arduino 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆