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

Jeebus
  • 3
  • 1

1 Answers1

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;

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81