Arduino无法编译名称为SP的变量:'Expected')'在'('token')之前. [英] Arduino cannot compile variable named SP: "Expected ')' before '(' token"

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

问题描述

我正在为一个项目创建正向和反向运动学模型,似乎无法解决此错误. 我对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;

实例化类型为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)

所以使用更类似的东西:

so use something more like:

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的变量:'Expected')'在'('token')之前.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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