Conflict between a class method and library define

Hello everyone,
I'm developing a stetch to use handle the comunication between an arduino master and another one that is the slave, using Modbus over RS485. This code will use ArduinoModbus library for it.
This code uses another library that I made that has two functions, name ON and OFF, that are called like className.ON() and className.OFF(). However, ArduinoModbus has #define ON 1 and #define OFF 0, this gimme the error bellow.
I find it really strange given that those methods are within my class.
Any help is appriciated.

In file included from C:\Program Files (x86)\Arduino\libraries\ArduinoModbus\src/ModbusClient.h:24:0,

                 from C:\Program Files (x86)\Arduino\libraries\ArduinoModbus\src/ModbusRTUClient.h:25,

                 from C:\Program Files (x86)\Arduino\libraries\ArduinoModbus\src/ArduinoModbus.h:23,

                 from C:\Users\user\AppData\Local\Temp\Temp1_CustomLibrary.zip\CustomLibrary\examples\ModbusRTUMasterHoldingRegister\ModbusRTUMasterHoldingRegister.ino:22:

C:\Program Files (x86)\Arduino\libraries\ArduinoModbus\src/libmodbus/modbus.h:58:12: error: expected unqualified-id before numeric constant

 #define ON 1

            ^

C:\Program Files (x86)\Arduino\libraries\CustomLibrary\src/CustomLibrary.h:30:10: note: in expansion of macro 'ON'

     void ON(uint8_t_pin);

          ^~

C:\Program Files (x86)\Arduino\libraries\ArduinoModbus\src/libmodbus/modbus.h:54:13: error: expected unqualified-id before numeric constant

 #define OFF 0

             ^

C:\Program Files (x86)\Arduino\libraries\CustomLibrary\src/CustomLibrary.h:46:10: note: in expansion of macro 'OFF'

     void OFF(uint8_t_pin);

          ^~~

C:\Users\user\AppData\Local\Temp\Temp1_CustomLibrary.zip\CustomLibrary\examples\ModbusRTUMasterHoldingRegister\ModbusRTUMasterHoldingRegister.ino: In function 'void setup()':

C:\Program Files (x86)\Arduino\libraries\ArduinoModbus\src/libmodbus/modbus.h:58:12: error: expected unqualified-id before numeric constant

 #define ON 1

            ^

C:\Users\user\AppData\Local\Temp\Temp1_CustomLibrary.zip\CustomLibrary\examples\ModbusRTUMasterHoldingRegister\ModbusRTUMasterHoldingRegister.ino:34:12: note: in expansion of macro 'ON'

   className.ON(1);

            ^~

C:\Program Files (x86)\Arduino\libraries\ArduinoModbus\src/libmodbus/modbus.h:58:12: error: expected unqualified-id before numeric constant

 #define ON 1

            ^

C:\Users\user\AppData\Local\Temp\Temp1_CustomLibrary.zip\CustomLibrary\examples\ModbusRTUMasterHoldingRegister\ModbusRTUMasterHoldingRegister.ino:36:12: note: in expansion of macro 'ON'

   className.ON(2);

            ^~

Easiest to change the names in your class. eg:
.turnOn()
.turnOff()

Or undefine the macros just after the #include of the Modbus library:

#undef ON
#undef OFF

That will allow you to use the names ON and OFF.

Thanks for the answers, but what I want to know is WHY it happens since the methods are inside a class.

#define is a preprocessor feature and will do its substitution without any regard for where it finds the text it needs to operate on. Being in a class is an irrelevance.

Thanks for the answer!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.