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.

ioan ghip
  • 1,202
  • 2
  • 16
  • 27
  • 1
    Possible duplicate of [ISO C++ forbids declaration of "something" with no type](http://stackoverflow.com/questions/18670953/iso-c-forbids-declaration-of-something-with-no-type). Just add a forward declaration: `template class value;` – paulsm4 Mar 09 '16 at 18:43
  • @paulsm4 I still get the same error even if I add a forward declaration. – ioan ghip Mar 09 '16 at 19:05
  • @Niall Here is the whole code so far (I'm developing it right now so isn't done): http://pastebin.com/24cY69e8 Also, if I comment out the template function code, everything compiles fine. – ioan ghip Mar 09 '16 at 19:14
  • 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. – Soundararajan Mar 09 '16 at 19:17
  • BTW, it fails for me with the same error inside arduino ide as well. verison 1.6.2 – Soundararajan Mar 09 '16 at 19:19
  • Move the template functions to before the two functions above them that use the templates. – Niall Mar 09 '16 at 19:19
  • @Soundararajan I'm using Arduino IDE v 1.6.8 Hourly Build – ioan ghip Mar 09 '16 at 19:23
  • @Soundararajan I'm not sure where do I find the compile command line that Arduino IDE uses. – ioan ghip Mar 09 '16 at 19:26
  • @Niall still getting the same error even if I move the template functions before the functions that uses the templates. – ioan ghip Mar 09 '16 at 19:27
  • To get command line from Arduino IDE : Arduino IDE -> File -> Preferences -> "Select verbose output for" ->Compilation. – Soundararajan Mar 09 '16 at 19:29
  • 1
    It maybe something in the tool chain ... http://stackoverflow.com/q/18052310/3747990 – Niall Mar 09 '16 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 09 '16 at 20:08

2 Answers2

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.

Soundararajan
  • 2,000
  • 21
  • 23
2

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");
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190