-1

I'm a little dumbfounded as to why this isn't working. I keep getting the following linker error:

In file included from src\ac.cpp:1:0:
src/ac.h:9:21: error: expected identifier before numeric constant
 #define RECEIVE_PIN 0
                     ^
src/ac.h:14:26: note: in expansion of macro 'RECEIVE_PIN'
     static IRrecv irrecv(RECEIVE_PIN);
                          ^
src/ac.h:9:21: error: expected ',' or '...' before numeric constant
 #define RECEIVE_PIN 0
                     ^
src/ac.h:14:26: note: in expansion of macro 'RECEIVE_PIN'
     static IRrecv irrecv(RECEIVE_PIN);
                          ^
*** [.pio\build\esp32cam\src\ac.cpp.o] Error 1
*** [.pio\build\esp32cam\src\main.cpp.o] Error 1

I am simply trying to initialize the IR receiver with static IRrecv irrecv(RECEIVE_PIN);, but the compiler is refusing to link it together.

#ifndef _AC_H_
#define _AC_H_

#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>

#define RECEIVE_PIN 0

class IR
{
public:
    static IRrecv irrecv(RECEIVE_PIN);
    static decode_results results;
    void initRecv();
    bool waitForIR();
};

extern IR ir;

#endif

If I do static IRrecv irrecv; it successfully compiles, but I'm unsure if I can pass the pin variable after the fact.

4
  • 1
    stackoverflow.com/questions/5019856/…
    – Mat
    Commented Jan 2, 2022 at 16:41
  • 1
    what happens if you use RCV_PIN? ... looks like RECEIVE_PIN is a macro
    – jsotola
    Commented Jan 2, 2022 at 16:52
  • @Mat It doesn't seem to be possible to change the value after initializing it
    – Jeebus
    Commented Jan 2, 2022 at 17:00
  • @jsotola I've rewritten it in various formats. I've tried all kinds of data types, names, and initializing it as irrecv(0) as well.
    – Jeebus
    Commented Jan 2, 2022 at 17:00

1 Answer 1

2

Static data members have to be defined outside the class declaration. The initialization happens at the point where the members are defined:

file ac.h:

//...

class IR
{
public:
    // Static data members are declared only:
    static IRrecv irrecv;
    static decode_results results;
    void initRecv();
    bool waitForIR();
};

file ac.cpp:

#include "ac.h"

#define RECEIVE_PIN 0

// Definitions and initializations of static data members:
IRrecv IR::irrecv(RECEIVE_PIN);
decode_results IR::results;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.