I'm trying to compile some code that compile fine in Arduino IDE, in Visual Studio with support for Arduino (Visual Micro). This is the code with problems:
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned 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;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return i;
}
The error I'm getting is:
app.ino:43:40: error: 'T' does not name a type
:int EEPROM_writeAnything(int ee, const T& value)
app.ino:43:43: error: ISO C++ forbids declaration of 'value' with no type [-fpermissive]
Can someone please point me in the right direction?
Thanks.
I think i got the answer. You need to add declaration for the functions in Visual Studio manually.
template <class T> int EEPROM_writeAnything(int ee, const T& value);
template <class T> int EEPROM_readAnything(int ee, T& value);
but whereas Arduino IDE preprocess your source code and adds these automatically for you behind the scene. So it works in Arduino IDE.
Hint : When you enable verbose output in your arduino IDE, refer to the temporary path in which the intermediate files generated during compilation is saved. It should be something like %temp%\build0094e6ca87558f1142f08e49b0685193.tmp\sketch . It should have the following statements .
#line 2 "C:\\Users\\Sound\\Documents\\Arduino\\sketch_mar10d\\sketch_mar10d.ino"
template <class T> int EEPROM_writeAnything(int ee, const T& value);
#line 11 "C:\\Users\\Sound\\Documents\\Arduino\\sketch_mar10d\\sketch_mar10d.ino"
template <class T> int EEPROM_readAnything(int ee, T& value);
#line 21 "C:\\Users\\Sound\\Documents\\Arduino\\sketch_mar10d\\sketch_mar10d.ino"
To know more about this, read here.
This snippet compiles fine under GCC/Linux and MSVS 2015/Windows.
Q: Does it work for you? Is it OK with the Arduino IDE?
Q: Does it fail with "error: 'T' does not name a type" with Arduino (Visual Micro)? Have you contacted Visual Micro?
#include <stdio.h>
typedef unsigned char byte;
class A {
public:
void write(int & ee, const byte &p) { }
};
A EEPROM;
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
return i;
}
int main (int argc, char *argv[]) {
printf ("Hello world\n");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With