4
\$\begingroup\$

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).

asked Oct 24, 2011 at 2:34
\$\endgroup\$
1
  • 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\$ Commented Oct 24, 2011 at 8:26

1 Answer 1

6
\$\begingroup\$

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.

answered Oct 24, 2011 at 10:56
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.