【问题标题】:Invalid constructor token C++ Arduino无效的构造函数令牌 C++ Arduino
【发布时间】:2015-06-17 04:49:45
【问题描述】:

我收到了错误

C:\Users\Jake\Documents\Arduino\libraries\Sonar\Sonar.cpp:3:13: error: expected >constructor, destructor, or type conversion before '(' token 声纳::声纳(trigLeft,echoLeft,trigRight,echoRight) 编译出错。

我不知道是什么原因造成的,曾经有一个花括号丢失,但我重新添加了它。代码如下

声纳.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 准备的,我相信它工作正常)

#include <Sonar.h>

Sonar sonar(20,21,22,23);
void setup() {
// put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
  sonar.Ping();
  long sonarLeftDistance = sonar.cmLeft;
  long sonarRightDistance = sonar.cmRight;

}

这就是所有的代码。 Arduino 位在很大程度上无关紧要。

【问题讨论】:

  • 你可能打算写 Sonar::Sonar(int trigLeft,int echoLeft,int trigRight, int echoRight) :-P ... 从你的声明中复制具有正确类型的函数签名,就像这样简单。
  • 这不是 "brilliant",而是非常非常基本的东西(我什至觉得回答它很愚蠢)。 'code' 不是错误消息,您可能应该问另一个问题,因为这似乎是一个不相关的问题。
  • 对不起,我对 SO 比较陌生,刚从大学毕业,主要是问讲师/MSDN,因为 C#..
  • 我现在刚刚得到“C:\Users\Jake\Documents\Arduino\libraries\Sonar\Sonar.cpp:13:15: 错误:分配只读成员 'Sonar::echoPinRight' echoPinRight = echoRight; ^ 编译错误。”对于每个数据成员,如您所知,我不擅长 c++,更多的是 C 和 C#...
  • 决定我只是不太关心它们是 const 的,使它们成为正常的整数,现在很好。无论如何,它们永远不会重新分配,它们只是 arduino 上的引脚。非常感谢你的帮助。对不起,如果我尴尬地愚蠢,我只是不知道该怎么做。我知道方法需要类型,只是大脑被锁定在寻找花括号。我猜 C# 让一切变得太容易了 :)

标签: c++ arduino


【解决方案1】:

你错过了根据你的构造函数声明在你的函数定义中指定参数类型:

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

Sonar::Sonar(int trigLeft, int echoLeft, int trigRight, int echoRight) {
          // ^^^           ^^^           ^^^            ^^^
    // ...
}

由于对应的类成员变量声明为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) {
}

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

【讨论】:

  • 老兄,你摇滚,我现在都修好了。非常感谢你,我知道这看起来很简单,但对我来说你是英雄,我保证
  • 我的机器人突然像老板一样绕着物体转动。突然:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多