C++ 无效的构造函数令牌C++;阿杜伊诺

C++ 无效的构造函数令牌C++;阿杜伊诺,c++,arduino,C++,Arduino,我发现了错误 C:\Users\Jake\Documents\Arduino\libraries\Sonar\Sonar.cpp:3:13:错误:应为“(”标记之前的构造函数、析构函数或类型转换 声纳:声纳(trigLeft、echoLeft、trigRight、echoRight) 编译时出错 我不知道是什么原因导致了这一点,有一个大括号丢失了,但我重新添加了它 Sonar.cpp: #include "Sonar.h" Sonar::Sonar(trigLeft,echoLeft,trig

我发现了错误

C:\Users\Jake\Documents\Arduino\libraries\Sonar\Sonar.cpp:3:13:错误:应为“(”标记之前的构造函数、析构函数或类型转换 声纳:声纳(trigLeft、echoLeft、trigRight、echoRight) 编译时出错

我不知道是什么原因导致了这一点,有一个大括号丢失了,但我重新添加了它

Sonar.cpp:

#include "Sonar.h"

Sonar::Sonar(trigLeft,echoLeft,trigRight, echoRight) {
    pinMode(triggerPinLeft,OUTPUT);
    pinMode(triggerPinRight,OUTPUT);
    pinMode(echoPinLeft,INPUT);
    pinMode(echoPinRight,INPUT);

    triggerPinLeft = trigLeft;
    echoPinLeft = echoRight;
    triggerPinRight = trigRight;
    echoPinRight = echoRight;

}



void Sonar::Ping() {

  digitalWrite(triggerPinLeft, LOW);
  digitalWrite(triggerPinRight, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPinLeft, HIGH);
  digitalWrite(triggerPinRight, HIGH);;
  delayMicroseconds(5);
  digitalWrite(triggerPinLeft, LOW);
  digitalWrite(triggerPinRight, LOW);

  // Read EchoPins

  long durationLeft = pulseIn(echoPinLeft, HIGH);
  long durationRight = pulseIn(echoPinRight, HIGH);

  // convert the time into a distance

  cmLeft = microsecondsToCentimeters(durationLeft);
  cmRight - microsecondsToCentimeters(durationRight);

  delay(100);
}

long Sonar::microsecondsToCentimeters(long microseconds) {
    // The speed of sound is 340 m/s or 29 microseconds per centimeter.

    return microseconds / 58;
}
声纳。h:

#ifndef Sonar_h
#define Sonar_h

#include "Arduino.h"

class Sonar {
  public:
    Sonar(int,int,int,int);
    long cmLeft,cmRight;
    void Ping();
  private:
    const int triggerPinLeft,echoPinLeft,triggerPinRight,echoPinRight;
    long microsecondsToInches(long microseconds);
    long microsecondsToCentimeters(long microseconds);
};

#endif
最后是Sonar.ino(这是给Arduino的,我相信它工作得很好)

#包括
声纳(20,21,22,23);
无效设置(){
//将安装代码放在此处,以便运行一次:
}
void循环(){
//将主代码放在此处,以便重复运行:
声纳;
long sonar LeftDistance=sonar.cmLeft;
long sonar RightDistance=sonar.cmRight;
}

这就是全部代码。Arduino位基本上是不相关的。

您没有根据构造函数函数声明在函数定义中指定参数类型:

Sonar(int,int,int,int); // Declaration

因为相应的类成员变量被声明为
const

    const int triggerPinLeft,echoPinLeft,triggerPinRight,echoPinRight;
 // ^^^^^  
需要使用构造函数成员初始值设定项列表对其进行初始化:

Sonar::Sonar(int trigLeft, int echoLeft, int trigRight, int echoRight) 
: triggerPinLeft(trigLeft)
, echoPinLeft(echoLeft)
, triggerPinRight(trigRight)
, echoPinRight(echoRight) {
}

这是你能做到的唯一方法。

你可能想写
Sonar::Sonar(int-trigLeft,int-echoLeft,int-trigRight,int-echoRight)
:-p…用声明中的正确类型复制函数签名,就像那样简单。这不是“出色”,而是非常非常基本的东西(我甚至觉得回答它很愚蠢)“代码”不是一条错误信息,你可能应该问另一个问题,因为这似乎是一个无关的问题。很抱歉,我对这个问题比较陌生,刚从大学出来,主要是问讲师/MSDN,因为C#……我现在才开始“C:\Users\Jake\Documents\Arduino\libraries\Sonar\Sonar.cpp:13:15:错误:分配只读成员'Sonar::echoPinRight'echoPinRight=echoRight;^编译错误。"对于每个数据成员,我不太擅长C++,正如你可以说的那样,C和C……我决定不关心他们是const的,使它们成为正常的int型,现在是好的。无论如何,它们永远不会重新分配,不管怎样,它们都是阿杜伊诺的别针。非常感谢你们的帮助。对不起,如果我是个笨拙的哑巴,我只是不知道该怎么办。.我确实知道方法需要类型,只是大脑一直在寻找一个卷曲的支架。我想C#让一切变得太简单了:)哥们,你太棒了,我现在都修好了。非常感谢你们,我知道这看起来很简单,但对我来说你们是一个英雄,我保证,我的机器人会突然像个老板一样在物体周围控制自己。突然间:D
    const int triggerPinLeft,echoPinLeft,triggerPinRight,echoPinRight;
 // ^^^^^  
Sonar::Sonar(int trigLeft, int echoLeft, int trigRight, int echoRight) 
: triggerPinLeft(trigLeft)
, echoPinLeft(echoLeft)
, triggerPinRight(trigRight)
, echoPinRight(echoRight) {
}