3

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.

12
  • 1
    Possible duplicate of ISO C++ forbids declaration of "something" with no type. Just add a forward declaration: template <class T> class value;
    – paulsm4
    Mar 9, 2016 at 18:43
  • @paulsm4 I still get the same error even if I add a forward declaration.
    – ioan ghip
    Mar 9, 2016 at 19:05
  • 1
    what version of arduino ide did you use ? Can you paste the compilation command line when compiled from Arduino and when compiled from Visual studio. I would guess they are different. Visual Micro just wraps the underlying gcc compiler used in Arduino. So if it works in Arduino IDE it should work in Viusal studio as well. Mar 9, 2016 at 19:17
  • 1
    It maybe something in the tool chain ... stackoverflow.com/q/18052310/3747990
    – Niall
    Mar 9, 2016 at 19:38
  • 1
    @Niall, yes. I followed the advice in that post to create a header file... and it looks like that works. Thanks!
    – ioan ghip
    Mar 9, 2016 at 20:08

2 Answers 2

6

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.

0
1

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");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.