Skip to main content
deleted 9 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Template'd Templated class: How to create instance and work with it?

I have createcreated a class like this:

#ifndef MyList_h
#define MyList_h

#include "Arduino.h"

template <typename T>
class MyList {
public:  
  MyList(void); 
  ~MyList(void);
  
  void addItem(T* item);

  //more stuff
  
};

#endif

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:

list->addItem(i);

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 ( II know, I could update  ...) + Arduino Nano Board.

Template'd class: How to create instance and work with it?

I have create a class like this:

#ifndef MyList_h
#define MyList_h

#include "Arduino.h"

template <typename T>
class MyList {
public:  
  MyList(void); 
  ~MyList(void);
  
  void addItem(T* item);

  //more stuff
  
};

#endif

And implementation 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:

list->addItem(i);

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

Templated class: How to create instance and work with it?

I have created a class like this:

#ifndef MyList_h
#define MyList_h

#include "Arduino.h"

template <typename T>
class MyList {
public:  
  MyList(void); 
  ~MyList(void);
  
  void addItem(T* item);

  //more stuff
  
};

#endif

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:

list->addItem(i);

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.

Source Link
Alex
  • 121
  • 1
  • 5

Template'd class: How to create instance and work with it?

I have create a class like this:

#ifndef MyList_h
#define MyList_h

#include "Arduino.h"

template <typename T>
class MyList {
public:  
  MyList(void); 
  ~MyList(void);
  
  void addItem(T* item);

  //more stuff
  
};

#endif

And implementation 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:

list->addItem(i);

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