I have created a class like this:

#ifndef MyList_h
#define MyList_h

#include "Arduino.h"

template <typename T>
class MyList {

  void addItem(T* item);

  //more stuff



And implementation is like this:

#include "Arduino.h"
#include "MyList.h"

template<typename T> MyList<T>::MyList(void) {
  // constructor implementation here

template<typename T> MyList<T>::~MyList(void) {
  // desctructor implementation here

template<typename T> void MyList<T>::addItem(T* item) {
  // function implementation here

// ...more stuff

In my .ino Sketch I'm doing this (found this solution in a C++ example):

#include "MyList.h"
MyList<word>* list = new MyList<word>(); // global scope
//... more stuff

But when compiling, Arduino IDE complains:

Test.ino:5: undefined reference to `MyList::MyList()'

I googled a bit and found this alternative way for creating the instance:

ArrayList<word>* list;

Then there's no error, just compiles file. BUT as soon as I want to use this instance (=adding code) like this:


compiler complains again:

Test.ino:25: undefined reference to `MyList::addItem(unsigned int*)'

What's wrong? Why is this reference undefined?

I tried another way:

ArrayList<word> list; // again global scope

But then the compiler also tells me:

Test.ino:5: undefined reference to MyList<unsigned int>::MyList()' /tmp/ccp8qmCz.ltrans0.ltrans.o: In function _GLOBAL__sub_D_list': /....../Test.ino:5: undefined reference to `MyList::~MyList()'

So, how can I create an instance of my class that uses a template?

Environment: Arduino IDE 1.8.3 (I know, I could update...) + Arduino Nano Board.

  • You need to combine the header and source into a single header file. – Majenko Jan 17 '18 at 18:47

I think I found the solution:

According to: https://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor

--> Put all implementation in .h file. That's it. No more compler complains, code runs fine.

I think I have to dig deeper into c/c++ development ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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