Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

c++ - Arduino cannot compile variable named SP: "Expected ')' before '(' token"

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);
    }
};

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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

These two line:

Servo Shoulder();
Servo Elbow();

should probably be:

Servo Shoulder;
Servo Elbow;

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...