This code compiles and runs fine in version 2.1, but gives the following error in version 2.2.
EEPROM_template_test:1: error: ‘T’ has not been declared
EEPROM_template_test:2: error: ‘T’ has not been declared
Has something changed that I wasn’t aware of?
Is there a way to fix this?
#include <EEPROM.h>
template<class T>
void writeToEEPROM(int address, T& value)
{
union {
T type;
byte b[sizeof(T)];
} temp;
temp.type = value;
for (int i = 0; i < sizeof(T); i++)
{
EEPROM.write(address + i, temp.b[i]);
}
}
template<class T>
void readFromEEPROM(int address, T& value)
{
union {
T type;
byte b[sizeof(T)];
} temp;
for (int i = 0; i < sizeof(T); i++)
{
temp.b[i] = EEPROM.read(address + i);
}
value = temp.type;
}
void setup()
{}
void loop()
{
}
You will have to move the template definitions to a separate header file (Tab).
Delta_G
August 17, 2011, 10:34pm
#3
This is dissapointing. I had a lot of code that was working in 2.1 that doesn't work now. I may have to go back to 2.1 to avoid having to rewrite everything.
What's in the new version that is different?
Delta_G
August 17, 2011, 11:47pm
#5
Yeah, I saw that, but I don't see what is making my templates work differently. Does it have something to do with compiling the user sketch first?
This is what the IDE sends to the compiler…
#include <EEPROM.h>
#include "WProgram.h"
void writeToEEPROM(int address, T& value);
void readFromEEPROM(int address, T& value);
void setup();
void loop();
template<class T>
void writeToEEPROM(int address, T& value)
{
union {
T type;
byte b[sizeof(T)];
} temp;
temp.type = value;
for (int i = 0; i < sizeof(T); i++)
{
EEPROM.write(address + i, temp.b[i]);
}
}
template<class T>
void readFromEEPROM(int address, T& value)
{
union {
T type;
byte b[sizeof(T)];
} temp;
for (int i = 0; i < sizeof(T); i++)
{
temp.b[i] = EEPROM.read(address + i);
}
value = temp.type;
}
void setup() { }
void loop() { }
The IDE is not correctly prototyping the templates.