undefined reference to class method

Hi,

I try to build a class method that will accept any datatype as parameter. I use a template like this in my .cpp file:

template <typename DataStruct> bool EEPROM_Manager::write_anythingEE(int addr, DataStruct wert) {
  return eeprom_write_anything(addr, wert);
}

the method calls a private method:

template <typename DataStruct> bool EEPROM_Manager::eeprom_write_anything(int addr, DataStruct wert) {
  EEPROM.put(addr,wert);
  return true;
}

I can access other methods of my class. The definitions in my header file:

class EEPROM_Manager {
  private:
   template <typename DataStruct> bool eeprom_write_anything(int addr, DataStruct wert);
...
  public:
    EEPROM_Manager();
...
   template <typename DataStruct> bool write_anythingEE(int addr, DataStruct wert);
};

In the .ino file I call the method like this:

  EEPROM_Manager myWriter;
  
  PERSON somePerson;

  strcpy(somePerson.firstname, "Piet");
  strcpy(somePerson.lastname, "Pietersen");
  somePerson.phonenumber = 123456789;
  strcpy(somePerson.address, "Here and there");

  myWriter.write_anythingEE(0,somePerson);

This returns an undefined reference error:

Arduino: 1.8.8 (Mac OS X), TD: 1.45, Board: "Teensy 3.2 / 3.1, Serial, 96 MHz (overclock), Faster, US English"

.../eeprom_writer.ino.cpp.o: In function `setup':
.../eeprom_writer.ino:33: undefined reference to `bool EEPROM_Manager::write_anythingEE<PERSON>(int, PERSON)'

Seems to me that the compiler is looking for a method with the type PERSON as parameter ignoring the use of the template?!

Any ideas?

Template definitions must be available at compile time. Your header only contains the declarations, not the definitions.

When compiling the source file of the library, the compiler doesn't instantiate any template functions. That would be completely pointless, because there are millions of possible types, but it doesn't know what types you are going to use in your .ino file, because it's a separate translation/compilation unit.

Either move the template function definitions in the header file, or move them in an .ipp file, and include that .ipp file at the bottom of your header.

Pieter

thanks a lot - worked fine!