I'm trying to implement a C++ templated member function in Arduino 0022, but I'm getting an error in code which seems correct to me.

// in Settings.h
template <class T> void save( T variable );

// in Settings.cpp
template <class T> void Settings::save( T variable ) {
  Serial.println("Want to save a variable of size " + String( sizeof(T) ) );

// compiler (linker) output TimeMachineArduino.cpp.o: In function SimpleScreen::left():

SimpleScreen.h:85: undefined reference to void Settings::save<int>(int)

SimpleScreen.h:86: undefined reference to void Settings::save<double>(double)

SimpleScreen.h:87: undefined reference to void Settings::save<char>(char)

The SimpleScreen::left() function is where I'm implicitly instantiating the template functions (by calling save on an int, double, and char).

  • 2
    \$\begingroup\$ This is actually a C++ question. You should ask such questions on Stackoverflow.com. The answer to this question is here: stackoverflow.com/q/648900 \$\endgroup\$
    – sharptooth
    Oct 24, 2011 at 8:26

1 Answer 1


You need to move the contents of the .cpp file into the .h file. Separating declarations and definitions doesn't work for templates. See the link @sharptooth posted: https://stackoverflow.com/q/648900.


Your Answer

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

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