首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未在此范围内声明。不管我怎么努力,我都被困住了

未在此范围内声明。不管我怎么努力,我都被困住了
EN

Stack Overflow用户
提问于 2022-03-16 07:54:15
回答 1查看 612关注 0票数 1

这可能很简单,但我现在正把头撞在墙上。我试图将一个简单的程序分割成多个文件,这样我就可以添加到其中了。我在逻辑上把它分成几个部分。如果有人能给我指点我的错误,那就太棒了。

Main.cpp

代码语言:javascript
复制
#include <Arduino.h>
#include "measure.h"
#include "LoRa.h"

void setup()
{
    Serial.begin(9600);  // STart serial for monitoring/debugging
    Serial2.begin(9600); // Start 2nd serial interface for use with EBYTE module
    Serial.println("Starting Reader");
    Serial.println(Transceiver.init()); // This init will set the pinModes for you
    Transceiver.PrintParameters();      // Display parameters of EBYTE (Can be changed. See example sketches)
}
//=================================================================
//======LOOP=======================================================
//=================================================================

void loop()
{
    measure();  // Take measurements
    delay(1000); // For testing. Will not exist in final
}

measure.cpp

代码语言:javascript
复制
// Takes various measurements, updates struct and sends to receiver

#include <Arduino.h>
#include "measure.h"
#include "MyData.h"
#include "LoRa.h"

unsigned long Last;
void measure()
{
    // measure some data and save to the structure
    MyData.Count++;
    MyData.Bits = analogRead(A0);
    MyData.Volts = MyData.Bits * (5.0 / 1024.0);

    // Send struct
    Transceiver.SendStruct(&MyData, sizeof(MyData));
    // Let us know something was sent
    Serial.print("Sending: ");
    Serial.println(MyData.Count);
}

measure.h

代码语言:javascript
复制
#ifndef MEASURE_H
#define MEASURE_H

#include <Arduino.h>
#include "LoRa.h"

void measure();
#endif

LoRa.cpp

代码语言:javascript
复制
// Somewhere to define/declare everything to do with the Ebyte E32 LoRa module
#include <Arduino.h>
#include "EBYTE.h"
#include "LoRa.h"

#define PIN_RX 16 // Serial2 RX (connect this to the EBYTE Tx pin)
#define PIN_TX 17 // Serial2 TX pin (connect this to the EBYTE Rx pin)
#define PIN_M0 4  // D4 on the board (possibly pin 24)
#define PIN_M1 22 // D2 on the board (possibly called pin 22)
#define PIN_AX 21 // D15 on the board (possibly called pin 21)

// create the transceiver object, passing in the serial and pins
EBYTE Transceiver(&Serial2, PIN_M0, PIN_M1, PIN_AX);

LoRa.h

代码语言:javascript
复制
#ifndef LORA_H
#define LORA_H

#include "Arduino.h"

#endif

MyData.h

代码语言:javascript
复制
#ifndef MYDATA_H
#define MYDATA_H

struct DATA
{
  unsigned long Count;
  int Bits;
  float Volts;
  float Temp;
} MyData;

#endif

偏误

代码语言:javascript
复制
src\main.cpp: In function 'void setup()':
src\main.cpp:45:20: error: 'Transceiver' was not declared in this scope
     Serial.println(Transceiver.init()); // This init will set the pinModes for you
                    ^
src\measure.cpp: In function 'void measure()':
src\measure.cpp:18:5: error: 'Transceiver' was not declared in this scope
     Transceiver.SendStruct(&MyData, sizeof(MyData));
     ^
*** [.pio\build\esp32doit-devkit-v1\src\measure.cpp.o] Error 1
*** [.pio\build\esp32doit-devkit-v1\src\main.cpp.o] Error 1

谢谢!!

EN

回答 1

Stack Overflow用户

发布于 2022-03-16 13:44:39

您正在LoRa.cpp中创建一个LoRa.cpp对象,但它尚未被measure.cpp文件访问。要到达另一个文件中的文件范围中的对象,可以在声明中添加extern

代码语言:javascript
复制
extern EBYTE Transceiver;

此外,为了让measure.cpp文件了解什么类型的对象收发器(例如,它包含哪些方法),您应该在measure.cpp中包含EBYTE报头:

代码语言:javascript
复制
#include "EBYTE.h"

一个使用EBYTE码的示例。

例如:

代码语言:javascript
复制
#include <Arduino.h>
#include "EBYTE.h"
#include "measure.h"
#include "MyData.h"
#include "LoRa.h"

unsigned long Last;
extern EBYTE Transceiver;

void measure()
{
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71493403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档