Struggling with "not declared"

This is probably simple but I’m banging my head against the wall for sometime now. I’m trying to split a simple program into multiple files so I can add to it. I’m trying to logically split it into parts. If anyone can give me some pointers as to my mistakes that’d be awesome.

Main.cpp

#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

// 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

#ifndef MEASURE_H
#define MEASURE_H

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

void measure();
#endif

LoRa.cpp

// 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

#ifndef LORA_H
#define LORA_H

#include "Arduino.h"

#endif

MyData.h

#ifndef MYDATA_H
#define MYDATA_H

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

#endif

ERRORS

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

Thanks!!

Both main.cpp and measure.cpp don’t see a declaration of Transceiver. Thus, they don’t compile successfully.

Since Transceiver is defined (i.e. allocated and constructed) in LoRa.cpp, it makes sense to add the declaration in LoRa.h:

#ifndef LORA_H
#define LORA_H

#include "Arduino.h"
#include "EBYTE.h"

extern EBYTE Transceiver;

#endif
1 Like

Thanks for your reply manuelbl, much appreciated. So I actually tried that but ended up removing it as I still get errors. I’ve tried it again with the same errors however realized I still had #include “EBYTE.h” in LoRa.cpp. If I leave it in LoRa.cpp I get a redefinition error such as

In file included from include/LoRa.h:5:0,
                 from src\LoRa.cpp:4:
.pio\libdeps\esp32doit-devkit-v1\EBYTE/EBYTE.h:181:7: error: redefinition of 'class EBYTE'
 class EBYTE {
       ^
In file included from src\LoRa.cpp:3:0:
.pio\libdeps\esp32doit-devkit-v1\EBYTE/EBYTE.h:181:7: error: previous definition of 'class EBYTE'
 class EBYTE {
       ^
*** [.pio\build\esp32doit-devkit-v1\src\LoRa.cpp.o] Error 1

It only compiles if it is included in LoRa.h, not in both or not in LoRa.cpp

Could you kindly explain what happens here? I’d have thought the header guards would prevent such a thing?

If I have #include EBYTE.h inside LoRa.cpp instead of LoRa.h, I get

In file included from include/measure.h:5:0,
                 from src\main.cpp:1:
include/LoRa.h:7:8: error: 'EBYTE' does not name a type
 extern EBYTE Transceiver;
        ^
src\main.cpp: In function 'void setup()':
src\main.cpp:46:20: error: 'Transceiver' was not declared in this scope
     Serial.println(Transceiver.init()); // This init will set the pinModes for you
                    ^
*** [.pio\build\esp32doit-devkit-v1\src\main.cpp.o] Error 1
In file included from include/measure.h:5:0,
                 from src\measure.cpp:4:
include/LoRa.h:7:8: error: 'EBYTE' does not name a type
 extern EBYTE Transceiver;
        ^
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

I guess what I’m asking is, when should I include a header within a header vs a cpp file?

Cheers!

[EDIT]
Now that I think about it, I guess it is because there are no guards in LoRa.cpp? Is there a simple rule to help one decide whether to include a .h within the cpp vs its same named .h?

Sorry for all the questions, I think I’ve almost got this :slight_smile:

Most likely the guards (#ifndef / #define / ... / #endif) are missing in EBYTE.h. Is it your code or a third-party library?

You can probably get away by including EBYTE.h from LoRa.h only.

Thanks again. It is a third arty library so I will check it and see if that’s the case :slight_smile: