Error with template <typename...>

I have one error with linker
I try compile and link a new library, Three files .cpp. .h and .ino…but I have a liker error.

I dont understand why if the next code link ok
Code: [Select]

void setup() {
  // put your setup code here, to run once:

}
int i;
float j;
unsigned long tiempo_old;

void loop() {
  // put your main code here, to run repeatedly:
dato(i,2000);
i++;
dato(j,1000);
}



template <typename tipo>
void dato(tipo& dat, int tiempo)
{
  if (millis() > (tiempo_old + tiempo))
  {
    Serial.print(dat);
    tiempo_old = millis();
  }
}

This one compile and link ok

But if I try compile next code, in three files, one .cpp, .h and .ino, because is a part of a new library…

This is .cpp
Code: [Select]

template <typename tipo>
void V_DATOS::dato(tipo& dat, int tiempo)
{
  if (millis() > (tiempo_old + tiempo))
  { 
    Serial.print(dat);
    tiempo_old = millis();
  }
}

Tis is .h
Code: [Select]

class V_DATOS
{
public:

	V_DATOS(MCUFRIEND_kbv* prtTFT, TouchScreen* TouchScreen,TECLADO* Teclado);

	void ventana(int x, int y, int tipo_variable,int num_digitos,int tipo_marco, uint16_t f_col, uint16_t t_col);

	template <typename tipo>
	void dato(tipo& dat, int tiempo);

	float introducir_dato(float dat);
	int introducir_dato(int dat);

private:

	int pos_x;
	int pos_y;
	int pixel_x;
	int pixel_y;
	int tamano_x, tamano_y;
	int tamano_letra;
	uint16_t f_color, t_color;
	int tip_var;
	int digitos;
	
	unsigned long tiempo_old;
	bool teclado_on;
	
	bool Touch_getXY(void);

protected:

	MCUFRIEND_kbv* _tft;  // nuevo denominacion del puntero para esta libreria   
	TouchScreen* _ts;	// denminacion de puntero para la tactil
	TECLADO* _teclado;
	
};

I have the next link error

[color=#222222]ERROR[/color]
[color=#222222]Compiling 'Ejemplo_Ventana_Datos_MCUFRIEND' for 'ATmega2560 (Mega 2560) (Arduino Mega)'[/color]

[color=#222222]cc6kmpQU.ltrans0.ltrans.o*: In function main[/color]


[color=#222222]Error linking for board ATmega2560 (Mega 2560) (Arduino Mega)[/color]
[color=#222222]Build failed for project 'Ejemplo_Ventana_Datos_MCUFRIEND'[/color]
[color=#222222](.text.startup+0xc08): undefined reference to void V_DATOS::dato<float>(float&, int)[/color]

[color=#222222]collect2.exe*: error: ld returned 1 exit status[/color]

.ino

vent_datos_2.dato(dato_f,500);

If I write the code and declare like float and not template types the compiler make it good

The function in .ino do anything, is only a trial…
Excuse my bad english…How can I link this good? Where is my mistake?
Thank you so much

I try writing code again…and I have problems too, I dont understand what I do bad,

cpp. file
Code: [Select]

template <typename T>
void V_DATOS::dato(T dato, int tiempo)
{
	if (millis() > (tiempo_old + tiempo))
	{
		tiempo_old = millis();
		_tft->fillRect(pos_x + 2, pos_y + 2, tamano_x - 6, tamano_y - 6, f_color);
		_tft->setTextSize(1);
		_tft->setTextColor(t_color);
		_tft->setFont(&FreeSans9pt7b);
		_tft->setCursor(pos_x + 10, pos_y + 22);
		_tft->print(dato);
	}
}

.h file
Code: [Select]

template <typename T>
void dato(T dato, int tiempo);

.ino filei , j is int and dato_f is a float
Code: [Select]

vent_datos_1.dato<int>(j , 500);
vent_datos_2.dato<float>(dato_f,500);

when I compile I have this error…WHY?
Code: [Select]

Compiling 'Ejemplo_Ventana_Datos_MCUFRIEND' for 'ATmega2560 (Mega 2560) (Arduino Mega)'
 
ccbbnCrN.ltrans0.ltrans.o*: In function main

Error linking for board ATmega2560 (Mega 2560) (Arduino Mega)
Build failed for project 'Ejemplo_Ventana_Datos_MCUFRIEND'
   (.text.startup+0xbf0): undefined reference to void V_DATOS::dato<int>(int, int)
   (.text.startup+0xc02): undefined reference to void V_DATOS::dato<float>(float, int)
 
collect2.exe*: error: ld returned 1 exit status

what is my error? How do I must write my code?

The template function’s implementation must be in the .h file.

How? Why?
Is code for a library and have .h .cpp.....all code work ok if I use float or int but I need write two code similar....

algec:
Why?

Because that's the way it is with template functions.

Thank you, I will try It ando I Will day that is ok.
Thank you again.

All is ok now, I put all code in my .h file and compile perfectly.

Thank you very much, I learn all days, and this make me happy.

Glad to hear. The need to put the template implementation in the .h file makes sense if you think about it. All source code files are compiled separately and then stitched together by the linker. When you instantiate the template in a source file, the compiler only has that source file and the .h containing the template to work with. It can't pull in source code from the associated .cpp to build the specific version of the template requested.