c++arduinoesp32lora

library not in scope in class


I have some code which I am trying to split into modules, the original code which compiles is:

#include <LoRa.h>

void setup() {
  // put your setup code here, to run once:
    LoRa.setPins(18, 23, 26);

}

and putting it in a class works as well in the main file, but splitting it into a header and implementation file always fails to build: Compilation error: 'LoRa' was not declared in this scope

the code is as follows: main.ino

#include "test.h"


void setup() {
    // put your setup code here, to run once:
  

}

void loop() {
  // put your main code here, to run repeatedly:

}

test.h

#ifndef LORA_H
#define LORA_H
#include <LoRa.h>


class test {

    public:
  
        test();
};

#endif

test.cpp

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


test::test() {

    LoRa.setPins(18, 23, 26);

}

I have rewrote this many times but it will just never build


Solution

  • Your test.h has the header guard for LoRa, and since your test.h gets included first, the LoRa.h file will be completely empty. (Since LORA_H was already defined, assuming it uses the LORA_H header guard).

    Try using a different header guard define than LORA_H:

    #ifndef TEST_H
    #define TEST_H
    #include <LoRa.h>