Change .ino to .cpp

Hi I'm studiing and testing templates in Arduino I wanted to use .cpp and .h files for creating Class Template but Arduino isn't so happy about this. If I use .ino instead of .cpp everything work fine, but if I use .cpp then folowing error shows up.

C:\Users\---\AppData\Local\Temp\ccF17MJ5.ltrans0.ltrans.o: In function `global constructors keyed to 65535_0_Templates.ino.cpp.o.1712':
<artificial>:(.text.startup+0x60): undefined reference to `Trida<2>::Trida(int)'
C:\Users\---\AppData\Local\Temp\ccF17MJ5.ltrans0.ltrans.o: In function `loop':
C:\Users\---\Documents\Arduino\studium\Templates/Templates.ino:12: undefined reference to `Trida<2>::blik()'
collect2.exe: error: ld returned 1 exit status
exit status 1
Nastala chyba při kompilaci u desky Arduino Uno.

my codes:

main file Templates.ino:

#include "Header.h"

Trida<2> objekt(13);

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  objekt.blik();
}

Header.h:

#ifndef templates_h
#define templates_h
#include <Arduino.h>

template<int N>
class Trida              //název třídy(objektu)
{
  public:                 //veřejné proměnné a funkce
    Trida(int source);     //konstruktor podle něj je tvořena instance třídy musí mít stejné jméno jako třída
    void blik();
  private:                //vnitřní proměnné a funkce ke kterým není přístup zvenčí
    int _var1;       // _ je označení pro vnitřní proměnnou není povinné ale slušné
    int pole[N];
};
#endif

and Trida.cpp:

#include "Header.h"

template<int N>
Trida<N>::Trida(int source)
{
  pinMode(source, OUTPUT);
  _var1 = source;
}

template<int N>
void Trida<N>::blik()
{
  digitalWrite(_var1,LOW);
  delay(1000);
  digitalWrite(_var1,HIGH);
  delay(1000);
}

I would like to move from Arduino IDE into PlatformIO so .ino files unfortunately are not answer for me. Did somebody met this problem here?

An answer here:

Just move the templates to Header.h.

NOTE: the more advanced approach mentioned in the linked answer of putting the template implementations in a .tpp or .ipp sketch file included from the .h file is not currently supported because these file extensions are not recognized in sketches. They are already supported in Arduino CLI and that may eventually make its way from there to the Arduino IDE.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.