Writing to the EEPROM

Hi, I’m trying to write to the Uno’s EEPROM using this code - http://forum.arduino.cc/index.php/topic,41497.0.html. However, I’m getting compile errors saying this,

C:\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -IC:\Arduino\hardware\arduino\cores\arduino -IC:\Arduino\hardware\arduino\variants\standard -IC:\Arduino\libraries\EEPROM C:\Users\Hrishi\AppData\Local\Temp\build33658092254607944.tmp\ReadWriteAnything.cpp -o C:\Users\Hrishi\AppData\Local\Temp\build33658092254607944.tmp\ReadWriteAnything.cpp.o 
ReadWriteAnything:4: error: expected ',' or '...' before '&' token
ReadWriteAnything:4: error: ISO C++ forbids declaration of 'T' with no type
ReadWriteAnything:5: error: 'T' has not been declared
ReadWriteAnything.ino: In function 'int EEPROM_writeAnything(int, const T&) [with T = byte]':
ReadWriteAnything.ino:30:   instantiated from here
ReadWriteAnything.ino:7: warning: comparison between signed and unsigned integer expressions
ReadWriteAnything.ino: In function 'int EEPROM_readAnything(int, T&) [with T = byte]':
ReadWriteAnything.ino:35:   instantiated from here
ReadWriteAnything.ino:16: warning: comparison between signed and unsigned integer expressions
ReadWriteAnything.ino: In function 'int EEPROM_writeAnything(int, const T&) [with T = long int]':
ReadWriteAnything.ino:44:   instantiated from here
ReadWriteAnything.ino:7: warning: comparison between signed and unsigned integer expressions
ReadWriteAnything.ino: In function 'int EEPROM_readAnything(int, T&) [with T = long int]':
ReadWriteAnything.ino:49:   instantiated from here
ReadWriteAnything.ino:16: warning: comparison between signed and unsigned integer expressions
ReadWriteAnything.ino: In function 'int EEPROM_writeAnything(int, const T&) [with T = st_t]':
ReadWriteAnything.ino:65:   instantiated from here
ReadWriteAnything.ino:7: warning: comparison between signed and unsigned integer expressions
ReadWriteAnything.ino: In function 'int EEPROM_readAnything(int, T&) [with T = st_t]':
ReadWriteAnything.ino:74:   instantiated from here
ReadWriteAnything.ino:16: warning: comparison between signed and unsigned integer expressions

This is the code I’m using,

#include <EEPROM.h>

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
  const byte* p = (const byte*)(const void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    EEPROM.write(ee++, *p++);
  return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
  byte* p = (byte*)(void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    *p++ = EEPROM.read(ee++);
  return i;
}

struct st_t {long lo; byte by; double db;};

void setup()
{
  Serial.begin(9600);
  int n = 0;

  byte by = 0x33;
  Serial.println(by, HEX);
  n = EEPROM_writeAnything(0, by);
  Serial.print("Wrote bytes: ");
  Serial.println(n);
  by = 0xFF;
  Serial.println(by, HEX);
  n = EEPROM_readAnything(0, by);
  Serial.print("Read bytes: ");
  Serial.println(n);
  Serial.println(by, HEX);

  Serial.println("-------");

  long lo = 0xDEADBEEF;
  Serial.println(lo);
  n = EEPROM_writeAnything(23, lo);
  Serial.print("Wrote bytes: ");
  Serial.println(n);
  lo = 0xFFFFFFFF;
  Serial.println(lo);
  n = EEPROM_readAnything(23, lo);
  Serial.print("Read bytes: ");
  Serial.println(n);
  Serial.println(lo);

  Serial.println("-------");

  double pi = 3.1415926538;

  struct st_t st;
  st.lo = 0xABADF00D;
  st.by = 0x22;
  st.db = pi;
  Serial.println(st.lo);
  Serial.println(st.by, HEX);
  Serial.println(st.db == pi);
  n = EEPROM_writeAnything(221, st);
  Serial.print("Wrote bytes: ");
  Serial.println(n);
  st.lo = 0xFFFFFFFF;
  st.by = 0x11;
  st.db = 0.0;
  Serial.println(st.lo);
  Serial.println(st.by, HEX);
  Serial.println(st.db == pi);
  n = EEPROM_readAnything(221, st);
  Serial.print("Read bytes: ");
  Serial.println(n);
  Serial.println(st.lo);
  Serial.println(st.by, HEX);
  Serial.println(st.db == pi);
}

void loop()
{
  //nothing to do here
}

Can anyone please help me sort this out?

Can anyone please help me sort this out?

You can’t be bothered searching the forum, I see. This problem has been well documented, along with the solution.

Put:

#include <EEPROM.h>

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
  const byte* p = (const byte*)(const void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    EEPROM.write(ee++, *p++);
  return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
  byte* p = (byte*)(void*)&value;
  int i;
  for (i = 0; i < sizeof(value); i++)
    *p++ = EEPROM.read(ee++);
  return i;
}

in a header file, and include that file in your sketch.