Pre-processor directive in class - undeclared error

I'm sure there's probably answers on Google to this question but I think I might be using the wrong term so I'm not finding anything relevant

I need to change the library I am importing depending on my microprocessor. I do it like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//platforms_ble.h
#if defined(ESP32) || defined(ESP8266)

#define ESP_BLE
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

#else

#define AVR_BLE
#include <SoftwareSerial.h>

#endif

#endif 

In there, depending on the MCU, I also define one of two variables. I use these in a class to change the definition of a variable and a function like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//utils.h
class CBLEController
{
    Callback m_cb;

#ifdef ESP_BLE
    BLEService *m_svce; //m_svce = m_service
#elif defined(AVR_BLE)
    SoftwareSerial m_dev; //m_dev = m_device
#endif 
public:
    CBLEController();
#ifdef ESP_BLE
    void setup(std::string name, std::string chUUID, std::string svUUID);

#elif defined(AVR_BLE)
    void setup(uint8_t rx, uint8_t tx, uint16_t baud = 9600);
#endif
    void createCallback(Callback cb) { m_cb = cb; };

    void begin();
    void end();
};

#endif 

In the cpp file, to go along with this I define the two setups. This is one:
1
2
3
4
5
6
7
void setup(uint8_t rx, uint8_t tx, uint16_t baud)
{
#ifdef AVR_BLE
    m_dev(rx, tx);
    m_dev.begin(baud);
#endif
}


But the moment I try and use either "m_svce" or "m_dev I get:
"error: 'm_dev'/'m_svce' was not declared in this scope"

What am I missing here?
Does utils.h #include platforms_ble.h?

In what you posted, platforms_ble.h is not included, so neither ESP_BLE nor AVR_BLE is defined, so neither m_svce nor m_dev is declared.
I should have added that - yes, utils.h does import the file.
Shouldn't it be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifdef AVR_BLE

void CBLEController::setup(uint8_t rx, uint8_t tx, uint16_t baud)
{
    // ...
}

#else

void CBLEController::setup(std::string name, std::string chUUID, std::string svUUID)
{
    // ...
}

#endif 

Last edited on
Ah I feel silly now. It was just staring me in the face!

Thanks for the answer, it's working now.
Registered users can post here. Sign in or register to post.