Arduino 无法编译名为 SP 的变量:“预期的 ')' 之前的'('令牌" [英] Arduino cannot compile variable named SP: "Expected ')' before '(' token"

查看:20
本文介绍了Arduino 无法编译名为 SP 的变量:“预期的 ')' 之前的'('令牌"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目的正向和反向运动学模型,但似乎无法修复此错误.我对 C++ 中的类很陌生,过去只在 python 中使用过它们,如果这是一个愚蠢的问题,我很抱歉.

I'm working on a forward and inverse kinematic model for a project and can't seem to fix this error. I am very new to classes in C++ and have only used them in python in the past so sorry if it's a stupid problem.

我的代码摘录如下,它是似乎给出错误的所有内容.显示错误的行被标记,我不知道发生了什么,我似乎无法修复它.

An extract of my code is below, which is everything involved in what seems to give the error. The line showing the error is labelled, I have no idea what is going on and I can't seem to fix it.

#include <Servo.h>


class Leg{
  public:
    //The class's variables that all functions for this class can use
    int PositionX; 
    int PositionY;

    Servo Shoulder();
    Servo Elbow();

    Leg(int SP, int EP){ //-----------This line has the error!
      // This is the constructor function
      const int ShoulderPin = SP;
      const int ElbowPin = EP;
      PositionX = 0
      PositionY = 0

      Shoulder.attach(ShoulderPin);
      Elbow.attach(ElbowPin);
    }

    void GoTo(float DemandS, float DemandE) {
      // Sends this Leg to a certain position (could make this return a True when it is done)
      // Inputs are in degrees (chould change)
      Shoulder.write(DemandS);
      Elbow.write(DemandE);
    }
};

我试过:给构造函数一个变量类型(void),使用 Leg::Leg(....{.到处检查任何未闭合的括号,都没有.注释掉 Servo 库及其所有用途.

I have tried: Giving the constructor function a variable type (void), Moving the constructor out of the code block using Leg::Leg(....{. Checking everywhere for any unclosed brackets, there are none. Commenting out the Servo library and all of its uses.

我真的很感激任何帮助,因为我觉得我已经尝试了所有方法并且一定在某处遗漏了一些东西,考虑在没有课程的情况下这样做,但这样做会很烦人.非常感谢:)

I'd really appretiate any help as I feel like I've tried everything and must be missing something somewhere, contemplating doing this without classes, but it will be very annoying to do that. Thanks vey much :)

推荐答案

这两行:

Servo Shoulder();
Servo Elbow();

应该是:

Servo Shoulder;
Servo Elbow;

i.e 实例化 Servo 类型的对象,而不是声明不带参数并返回 Servo 对象的函数.

i.e instantiating objects of type Servo rather than declaring functions that take no parameters and return a Servo object.

从评论中可以看出,您不应该在这里使用 SP 作为名称:

And from the comments it turns out that you shouldn't be using SP as a name here:

Leg(int SP, int EP)

所以使用更像:

Leg(int this_is_for_this, int and_this_is_for_this_other_thing)

或使用驼峰式大小写或代码中常见的任何内容,但要具有描述性.

or use camel case or whatever is common in your code but be descriptive.

这篇关于Arduino 无法编译名为 SP 的变量:“预期的 ')' 之前的'('令牌"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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