C++ _pin是一个全局变量,但我仍然得到错误:'_引脚';未在此范围中声明

C++ _pin是一个全局变量,但我仍然得到错误:'_引脚';未在此范围中声明,c++,arduino,visual-studio-code,platformio,C++,Arduino,Visual Studio Code,Platformio,编程语言:c++ 编译程序时,出现以下错误: /Button.cpp:50:18:错误:“\u pin”未在此范围内声明 它告诉我变量\u pin未在函数范围内声明 bool longPush(unsigned log interval),但是它被声明了,我甚至在其他函数中使用了\u pin,没有问题 以下是导致我出现问题的库按钮文件: 按钮h: \ifndef按钮 #定义按钮 #包括 类按钮 { 私人: 字节_pin; 字节反跳; 布尔伊斯坦特; 布尔推; 公众: 枚举PullRes:bool

编程语言:c++

编译程序时,出现以下错误:
/Button.cpp:50:18:错误:“\u pin”未在此范围内声明

它告诉我变量
\u pin
未在函数范围内声明
bool longPush(unsigned log interval)
,但是它被声明了,我甚至在其他函数中使用了
\u pin
,没有问题

以下是导致我出现问题的库
按钮
文件:

按钮h:
\ifndef按钮
#定义按钮
#包括
类按钮
{
私人:
字节_pin;
字节反跳;
布尔伊斯坦特;
布尔推;
公众:
枚举PullRes:bool{FLOATING=false,PULLUP=true,};
按钮(字节引脚,字节防跳,PullRes=浮动);
bool falling();
bool-rissing();
布尔检查();
bool longPush(无符号长间隔);
};
#恩迪夫
Button.cpp:
#包括
#包括
#包括
按钮::按钮(字节引脚,字节防跳,PullRes模式=浮动){
_引脚=引脚;
_反弹跳=反弹跳;
如果(模式==上拉)引脚模式(引脚,输入\上拉);
else引脚模式(引脚,输入);
}
bool按钮::rising(){//function que retrona un 1 cuando se detecta un rissing en el pin
布尔普尔斯;
如果((数字读取(_pin)==1)和(_est_ant==0)){
脉冲=1;
_est_ant=1;
}
否则{
脉冲=0;
_est_ant=数字读取(_pin);
}
延迟(反跳);
返回脉冲;
}
bool按钮::falling(){//function que retrona un 1 cuando se detecta un falling en el pin
布尔普尔斯;
如果((数字读取(_pin)==0)和(_est_ant==1)){
脉冲=1;
_est_ant=0;
}
否则{
脉冲=0;
_est_ant=数字读取(_pin);
}
延迟(反跳);
返回脉冲;
}
bool按钮::check(){//function que retrona el estado实际del pin
返回数字读取(_pin);
}
bool longPush(无符号长间隔){
静态定时器(间隔);
定时器。设置间隔(间隔);
静态布尔释放=真;
如果(数字读取(_pin)){
timer.end();
释放=真;
返回false;
}如果(!timer.isRunning()&&released),则为else{
释放=假;
timer.init();
返回false;
}else if(timer.read()){
timer.end();
释放=假;
返回true;
}
返回false;
}
.cpp文件中的错误:

bool longPush(unsigned long interval){
  static Timer timer(interval);
  timer.setInterval(interval);
  static bool released = true;
////////////////////////////////////////
  if(digitalRead(_pin)){// <--- right here
    timer.end();
    released = true;
    return false;
  } else if (!timer.isRunning() && released){
    released = false;
    timer.init();
    return false;
  } else if (timer.read()){
    timer.end();
    released = false;

    return true;
  }
return false;
}
bool longPush(无符号长间隔){
静态定时器(间隔);
定时器。设置间隔(间隔);
静态布尔释放=真;
////////////////////////////////////////
if(digitalRead(_pin)){/
\u pin
不是全局变量;它是成员变量

您试图将它当作一个全局变量来使用,因为您定义的是
longPush
(一个全局函数),而不是
Button::longPush
(一个成员函数)

在这里:

其他函数没有这个问题,因为它们的定义没有这种类型

不过,这是一个很容易犯的错误:就在今天,我至少做了两次!

\u pin
不是一个全局变量;它是一个成员变量

您试图将它当作一个全局变量来使用,因为您定义的是
longPush
(一个全局函数),而不是
Button::longPush
(一个成员函数)

在这里:

其他函数没有这个问题,因为它们的定义没有这种类型


不过,这是一个很容易犯的错误:就在今天,我至少做了两次!

我没有看到一个名为
\u pin
的全局变量
按钮
有一个名为
\u pin
的成员,但你不能访问
按钮
之外的
我没有看到一个名为
按钮
的全局变量有一个成员MeD代码> LewisMojica PIN <代码>但你不能访问外部<代码>按钮>代码>解决!我开始学习C++,看起来我必须对全局变量和成员变量之间的差异做一些研究。非常感谢。@你使用哪本书?没有。我在YouTube上找到了一个很好的教程,一旦我知道了基础知识就可以了。继续学习books@LewisMojica先从书开始,而不是从互联网开始。除非你已经很好地理解了什么是“是”,“不是”好的C++,你怎么能知道你正在跟随的教程写得好,值得跟随吗?如果不知道正确的术语来查找,你将如何去搜索WH?你需要知道吗?解决这两个问题。@ USER481301你能给我推荐一本书吗?这是有帮助的解决方法!我开始学习C++,看起来我必须对全局变量和成员变量之间的差异做一些研究。非常感谢。@ LewisMojica你在用哪本书?没有。我在YouTube W上找到了一个很好的教程。一旦我知道了基础知识,我会继续学习books@LewisMojica从书,而不是互联网。除非你已经对什么是好的和不好的C++有了很好的理解,你如何才能告诉你你正在跟随的教程写得好,值得跟随?如果不知道正确的术语来查找,Wi是怎样的?你能搜索你需要知道的吗?解决了这两个问题。@user4581301你能给我推荐一本书吗?那会很有帮助的
#include <Arduino.h>
#include <Button.h>
#include <Timer.h>

Button::Button(byte pin, byte anti_bounce, PullRes mode = FLOATING) {
  _pin = pin;
  _anti_bounce = anti_bounce;
  if(mode == PULLUP) pinMode(pin,INPUT_PULLUP);
  else pinMode(pin,INPUT);

}

  bool Button::rissing() {                                          //Funcion que retorna un 1 cuando se detecta un rissing en el pin <_pin>
  bool puls;
  if ((digitalRead(_pin) == 1) && (_est_ant == 0)) {
    puls = 1;
    _est_ant = 1;
  }
  else {
    puls = 0;
    _est_ant = digitalRead(_pin);
  }
  delay(_anti_bounce);
  return puls;
}

bool Button::falling() {                                          //Funcion que retorna un 1 cuando se detecta un falling en el pin <_pin>
  bool puls;
  if ((digitalRead(_pin) == 0) && (_est_ant == 1)) {
    puls = 1;
    _est_ant = 0;
  }
  else {
    puls = 0;
    _est_ant = digitalRead(_pin);
  }
  delay(_anti_bounce);
  return puls;
}

bool Button::check(){                                             //Funcion que retorna el estado actual del pin <_pin>
  return digitalRead(_pin);
}

bool longPush(unsigned long interval){
  static Timer timer(interval);
  timer.setInterval(interval);
  static bool released = true;

  if(digitalRead(_pin)){ 
    timer.end();
    released = true;
    return false;
  } else if (!timer.isRunning() && released){
    released = false;
    timer.init();
    return false;
  } else if (timer.read()){
    timer.end();
    released = false;

    return true;
  }
  return false;
}
bool longPush(unsigned long interval){
  static Timer timer(interval);
  timer.setInterval(interval);
  static bool released = true;
////////////////////////////////////////
  if(digitalRead(_pin)){// <--- right here
    timer.end();
    released = true;
    return false;
  } else if (!timer.isRunning() && released){
    released = false;
    timer.init();
    return false;
  } else if (timer.read()){
    timer.end();
    released = false;

    return true;
  }
return false;
}
   bool Button::longPush(unsigned long interval){
//      ^^^^^^^^