C++ “如何设置格式”;如果;C++;宏(&;如何使用Arduino&x27;的新EEPROM库get&;put函数)

C++ “如何设置格式”;如果;C++;宏(&;如何使用Arduino&x27;的新EEPROM库get&;put函数),c++,if-statement,arduino,macros,C++,If Statement,Arduino,Macros,2016年1月3日更新:我现在也回答了自己的问题 我需要一些帮助来格式化我在C++宏中的“IF”语句: #define updateEEPROMVal(address,val) if (EEPROM.get(address)!=val) \ EEPROM.put(address,val) 我得到了错误页面,所以我假设这是一个简单的格式问题 2020年4月更新:直接跳到。我的宏很好(用do{}包围它,而(false

2016年1月3日更新:我现在也回答了自己的问题

我需要一些帮助来格式化我在C++宏中的“IF”语句:
#define updateEEPROMVal(address,val)  if (EEPROM.get(address)!=val) \
                                        EEPROM.put(address,val)
我得到了错误页面,所以我假设这是一个简单的格式问题

2020年4月更新:直接跳到。我的宏很好(用
do{}包围它,而(false)
当然会更好,但它仍然很好)。我只是忘记了EEPROM中的第二个参数。第二个参数是通过非<代码> const C++引用传递的,而当时我并不真正知道引用是什么或它是如何工作的,所以我没有正确使用<代码>。就这样

回到我2016年的原始问题:

以下是完整的上下文:

//----------------------------------------------------------------------------------------------------------------------------------
//storeXYValsIntoEEPROM
//-store the current global variable x and y low, center, and high values into EEPROM
//----------------------------------------------------------------------------------------------------------------------------------
#define updateEEPROMVal(address,val)  if (EEPROM.get(address)!=val) \
                                        EEPROM.put(address,val)
void storeXYValsIntoEEPROM()
{
  //update EEPROM values *only* if necessary, this way you minimize writes (and wear-and-tear) on the EEPROM, since it is limited to
  //100k writes per cell I believe (see datasheet)
  updateEEPROMVal(0,x_low);
  updateEEPROMVal(2,x_ctr);
  updateEEPROMVal(4,x_high);
  updateEEPROMVal(6,y_low);
  updateEEPROMVal(8,y_ctr);
  updateEEPROMVal(10,y_high);
}
更新:以下是我的错误输出:

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino Uno"  

Using library IRremote in folder: C:\Gabe\Gabe - RC-extra\Arduino\Sketches\libraries\IRremote (legacy)  

Using library eRCaGuy_ButtonReader in folder: C:\Gabe\Gabe - RC-extra\Arduino\Sketches\libraries\eRCaGuy_ButtonReader (legacy)  

Using library EEPROM in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM   



C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -Wall -Wextra -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10605 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Gabe\Gabe - RC-extra\Arduino\Sketches\libraries\IRremote -IC:\Gabe\Gabe - RC-extra\Arduino\Sketches\libraries\eRCaGuy_ButtonReader -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM C:\Users\Gabriel\AppData\Local\Temp\build6111239347405487460.tmp\IR_Tx_code5_w_calibration_mode.cpp -o   C:\Users\Gabriel\AppData\Local\Temp\build6111239347405487460.tmp\IR_Tx_code5_w_calibration_mode.cpp.o   

In file included from IR_Tx_code5_w_calibration_mode.ino:27:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:43:30: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     operator const uint8_t() const       { return **this; }  
                              ^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:92:26: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
     operator const int() const          { return index; }  
                          ^
IR_Tx_code5_w_calibration_mode.ino: In function 'void storeXYValsIntoEEPROM()':
IR_Tx_code5_w_calibration_mode.ino:430:61: error: no matching function for call to 'EEPROMClass::get(int)'
IR_Tx_code5_w_calibration_mode.ino:436:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: note: candidate is:
IR_Tx_code5_w_calibration_mode.ino:436:3: note: in expansion of macro 'updateEEPROMVal'
In file included from IR_Tx_code5_w_calibration_mode.ino:27:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note: template<class T> T& EEPROMClass::get(int, T&)
     template< typename T > T &get( int idx, T &t ){  
                               ^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note:   template argument deduction/substitution failed:
IR_Tx_code5_w_calibration_mode.ino:430:61: note:   candidate expects 2 arguments, 1 provided
IR_Tx_code5_w_calibration_mode.ino:436:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: error: no matching function for call to 'EEPROMClass::get(int)'
IR_Tx_code5_w_calibration_mode.ino:437:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: note: candidate is:
IR_Tx_code5_w_calibration_mode.ino:437:3: note: in expansion of macro 'updateEEPROMVal'
In file included from IR_Tx_code5_w_calibration_mode.ino:27:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note: template<class T> T& EEPROMClass::get(int, T&)
     template< typename T > T &get( int idx, T &t ){  
                               ^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note:   template argument deduction/substitution failed:
IR_Tx_code5_w_calibration_mode.ino:430:61: note:   candidate expects 2 arguments, 1 provided
IR_Tx_code5_w_calibration_mode.ino:437:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: error: no matching function for call to 'EEPROMClass::get(int)'
IR_Tx_code5_w_calibration_mode.ino:438:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: note: candidate is:
IR_Tx_code5_w_calibration_mode.ino:438:3: note: in expansion of macro 'updateEEPROMVal'
In file included from IR_Tx_code5_w_calibration_mode.ino:27:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note: template<class T> T& EEPROMClass::get(int, T&)
     template< typename T > T &get( int idx, T &t ){  
                               ^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note:   template argument deduction/substitution failed:
IR_Tx_code5_w_calibration_mode.ino:430:61: note:   candidate expects 2 arguments, 1 provided
IR_Tx_code5_w_calibration_mode.ino:438:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: error: no matching function for call to 'EEPROMClass::get(int)'
IR_Tx_code5_w_calibration_mode.ino:439:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: note: candidate is:
IR_Tx_code5_w_calibration_mode.ino:439:3: note: in expansion of macro 'updateEEPROMVal'
In file included from IR_Tx_code5_w_calibration_mode.ino:27:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note: template<class T> T& EEPROMClass::get(int, T&)
     template< typename T > T &get( int idx, T &t ){  
                               ^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note:   template argument deduction/substitution failed:
IR_Tx_code5_w_calibration_mode.ino:430:61: note:   candidate expects 2 arguments, 1 provided
IR_Tx_code5_w_calibration_mode.ino:439:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: error: no matching function for call to 'EEPROMClass::get(int)'
IR_Tx_code5_w_calibration_mode.ino:440:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: note: candidate is:
IR_Tx_code5_w_calibration_mode.ino:440:3: note: in expansion of macro 'updateEEPROMVal'
In file included from IR_Tx_code5_w_calibration_mode.ino:27:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note: template<class T> T& EEPROMClass::get(int, T&)
     template< typename T > T &get( int idx, T &t ){  
                               ^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note:   template argument deduction/substitution failed:
IR_Tx_code5_w_calibration_mode.ino:430:61: note:   candidate expects 2 arguments, 1 provided
IR_Tx_code5_w_calibration_mode.ino:440:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: error: no matching function for call to 'EEPROMClass::get(int)'
IR_Tx_code5_w_calibration_mode.ino:441:3: note: in expansion of macro 'updateEEPROMVal'
IR_Tx_code5_w_calibration_mode.ino:430:61: note: candidate is:
IR_Tx_code5_w_calibration_mode.ino:441:3: note: in expansion of macro 'updateEEPROMVal'
In file included from IR_Tx_code5_w_calibration_mode.ino:27:0:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note: template<class T> T& EEPROMClass::get(int, T&)
     template< typename T > T &get( int idx, T &t ){  
                               ^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\EEPROM/EEPROM.h:130:31: note:   template argument deduction/substitution failed:
IR_Tx_code5_w_calibration_mode.ino:430:61: note:   candidate expects 2 arguments, 1 provided
IR_Tx_code5_w_calibration_mode.ino:441:3: note: in expansion of macro 'updateEEPROMVal'  
Multiple libraries were found for "IRremote.h"  

 Used: C:\Gabe\Gabe - RC-extra\Arduino\Sketches\libraries\IRremote  

 Not used: C:\Program Files (x86)\Arduino\libraries\RobotIRremote  

no matching function for call to 'EEPROMClass::get(int)'  
Arduino:1.6.5(Windows 8.1),板:“Arduino Uno”
使用文件夹C:\Gabe\Gabe-RC extra\Arduino\SKETS\libraries\IRremote(旧版)中的库IRremote
使用文件夹C:\Gabe\Gabe-RC extra\Arduino\SKETS\libraries\eRCaGuy\U按钮阅读器(旧版)
使用文件夹C:\Program Files(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM中的库EEPROM
C:\Program Files(x86)\Arduino\hardware\tools\avr/bin/avr-g++-C-g-Os-Wall-Wextra-fno exceptions-FFFunction sections-fdata sections-fno threadsafe statics-MMD-mmcu=atmega328p-DF_CPU=16000000L-DARDUINO=10605-DARDUINO_avr_UNO-DARDUINO_ARCH_avr-IC:\Program Files(x86)\Arduino\hardware\Arduino\Arduino\avr\cores\Arduino\IC:\IC程序文件(x86)\Arduino\hardware\Arduino\avr\variants\standard-IC:\Gabe\Gabe-RC extra\Arduino\Sketchs\libraries\IRremote-IC:\Gabe\Gabe-RC extra\Arduino\Sketchs\libraries\eRCaGuy\U按钮阅读器-IC:\Program Files(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM C:\Users\Gabriel\AppData\Local\Temp\Build611239347405487460.tmp\IR\u Tx\u code5\u w\u calibration\u mode.cpp-o C:\Users\Gabriel\AppData\Local\Temp\Build611239347405487460.tmp\IR\u Tx\u code5\u w\u calibration\u mode.cpp.o
在IR_Tx_代码5_w_校准_模式包含的文件中。ino:27:0:
C:\Program Files(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM/EEPROM.h:43:30:警告:函数返回类型[-Wignored限定符]上忽略类型限定符
运算符const uint8_t()const{return**this;}
^
C:\Program Files(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM/EEPROM.h:92:26:警告:函数返回类型[-Wignored限定符]上忽略类型限定符
运算符const int()const{return index;}
^
IR_Tx_code 5_w_calibration_mode.ino:在函数“void storexyvalsinoteeprom()”中:
IR_Tx_code 5_w_calibration_mode.ino:430:61:错误:调用“EEPROMClass::get(int)”时没有匹配函数
IR_Tx_code 5_w_calibration_mode.ino:436:3:注意:在宏'UpdateePromval'的扩展中
红外发射代码5 w校准模式。ino:430:61:注:候选项为:
IR_Tx_code 5_w_calibration_mode.ino:436:3:注意:在宏'UpdateePromval'的扩展中
在IR_Tx_代码5_w_校准_模式包含的文件中。ino:27:0:
C:\ProgramFiles(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM/EEPROM.h:130:31:注意:模板T&EEPROMClass::get(int,T&)
模板T&get(intidx,T&T){
^
C:\Program Files(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM/EEPROM.h:130:31:注意:模板参数推断/替换失败:
IR_Tx_code 5_w_calibration_mode.ino:430:61:注:候选者需要2个参数,提供1个参数
IR_Tx_code 5_w_calibration_mode.ino:436:3:注意:在宏'UpdateePromval'的扩展中
IR_Tx_code 5_w_calibration_mode.ino:430:61:错误:调用“EEPROMClass::get(int)”时没有匹配函数
IR_Tx_code 5_w_calibration_mode.ino:437:3:注意:在宏'UpdateePromval'的扩展中
红外发射代码5 w校准模式。ino:430:61:注:候选项为:
IR_Tx_code 5_w_calibration_mode.ino:437:3:注意:在宏'UpdateePromval'的扩展中
在IR_Tx_代码5_w_校准_模式包含的文件中。ino:27:0:
C:\ProgramFiles(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM/EEPROM.h:130:31:注意:模板T&EEPROMClass::get(int,T&)
模板T&get(intidx,T&T){
^
C:\Program Files(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM/EEPROM.h:130:31:注意:模板参数推断/替换失败:
IR_Tx_code 5_w_calibration_mode.ino:430:61:注:候选者需要2个参数,提供1个参数
IR_Tx_code 5_w_calibration_mode.ino:437:3:注意:在宏'UpdateePromval'的扩展中
IR_Tx_code 5_w_calibration_mode.ino:430:61:错误:调用“EEPROMClass::get(int)”时没有匹配函数
IR_Tx_code 5_w_calibration_mode.ino:438:3:注意:在宏'UpdateePromval'的扩展中
红外发射代码5 w校准模式。ino:430:61:注:候选项为:
IR_Tx_code 5_w_calibration_mode.ino:438:3:注意:在宏'UpdateePromval'的扩展中
在IR_Tx_代码5_w_校准_模式包含的文件中。ino:27:0:
C:\ProgramFiles(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM/EEPROM.h:130:31:注意:模板T&EEPROMClass::get(int,T&)
模板T&get(intidx,T&T){
^
C:\Program Files(x86)\Arduino\hardware\Arduino\avr\libraries\EEPROM/EEPROM.h:130:31:注意:模板参数推断/替换失败:
IR_Tx_code 5_w_calibration_mode.ino:430:61:注:候选者需要2个参数,提供1个参数
IR_Tx_code 5_w_calibration_mode.ino:438:3:注意:在宏'UpdateePromval'的扩展中
IR_Tx_code 5_w_calibration_mode.ino:430:61:错误:调用“EEPROMClass::get(int)”时没有匹配函数
IR_Tx_code 5_w_calibration_mode.ino:439:3:注意:在宏'UpdateePromval'的扩展中
红外发射代码5 w校准模式。ino:430:61:注:候选项为:
红外发射代码5 w校准模式。
 #define updateEEPROMVal(address,val)  do{if (EEPROM.get(address)!=val) \
                                    EEPROM.put(address,val);}while(0)
EEPROM.get(address);
any_type val_in_EEPROM;
EEPROM.get(address, val_in_EEPROM);
inline void updateEEPROMVal(uint16_t address, uint16_t val)
{
  uint16_t val_in_EEPROM;
  EEPROM.get(address,val_in_EEPROM);
  if (val_in_EEPROM!=val)
    EEPROM.put(address,val);
}

void storeGlobalXYValsIntoEEPROM()
{
  updateEEPROMVal(0,x_low);
  updateEEPROMVal(2,x_ctr);
  updateEEPROMVal(4,x_high);
  updateEEPROMVal(6,y_low);
  updateEEPROMVal(8,y_ctr);
  updateEEPROMVal(10,y_high);
}
uint16_t val_in_EEPROM;    
#define updateEEPROMVal(address,val)  EEPROM.get(address,val_in_EEPROM); \
                                      if (val_in_EEPROM!=val)            \
                                        EEPROM.put(address,val)
void storeGlobalXYValsIntoEEPROM()
{
  uint16_t val_in_EEPROM;

  updateEEPROMVal(0,x_low);
  updateEEPROMVal(2,x_ctr);
  updateEEPROMVal(4,x_high);
  updateEEPROMVal(6,y_low);
  updateEEPROMVal(8,y_ctr);
  updateEEPROMVal(10,y_high);
}