AnswerBun.com

Using a template inside a class

Arduino Asked by guyd on December 24, 2021

I’m trying to use a template function, but I get an error

home/guy/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-4-b40a506/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: sketch/emptyCode.ino.cpp.o: in function `startIOTservices()':
/home/guy/Documents/git/Arduino/HomePi/emptyCode/emptyCode.ino:37: undefined reference to `void myTest::funcOne<bool>(bool)'
/home/guy/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-4-b40a506/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: sketch/emptyCode.ino.cpp.o: in function `setup':
/home/guy/Documents/git/Arduino/HomePi/emptyCode/emptyCode.ino:70: undefined reference to `void myTest::funcOne<bool>(bool)'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

inside .cpp file:

myTest::myTest()
{
        Serial.println("start");
}

template <class T1> 
void myTest::funcOne(T1 arg)
{
        Serial.println(arg);
}

inside .h file:

class myTest
{
public:
    myTest();
    template <class T1> 
    void funcOne(T1 arg);
};

and calling it from .ino file:

testA.funcOne(true);

What am I doing wrong ?

EDIT 1:

complete .h .cpp files – which originaly belongs to myJSON library which are deleted in this snip

#ifndef myJSON_h
#define myJSON_h

#include "Arduino.h"
#include <ArduinoJson.h>
#include "FS.h"

#define DOC_SIZE 1000

class myJSON
{
};

class myTest
{
public:
    myTest();
    template <class T1> 
    void funcOne(T1 arg);
};

#endif

.cpp file

#include "Arduino.h"
#include "myJSON.h"
#include "FS.h"
#include <ArduinoJson.h>

#define LOG_LENGTH 4

myJSON::myJSON(char *filename, bool useserial)
{
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

myTest::myTest()
{
        Serial.println("start");
}

// template <typename T1> 
void myTest::funcOne(T1 arg)
{
        Serial.println(arg);
}

One Answer

move the template method definiton to .h or define it in class. it is not a real function, only a prescription for functions. Like this:

class myTest
{
public:
    myTest();
    template <class T1>
    void funcOne(T1 arg);
};

template <class T1>
void myTest::funcOne(T1 arg)
{
        Serial.println(arg);
}

or this

class myTest
{
public:
    myTest();
    template <class T1>
    void funcOne(T1 arg)
    {
            Serial.println(arg);
    }
};

Answered by Juraj on December 24, 2021

Add your own answers!

Related Questions

White screen when I perform grapicstest in ili9341

1  Asked on February 16, 2021 by prashant

 

How to avoid MQTT reconnect Loop

0  Asked on February 15, 2021

   

loop once per startup for stepper motor

0  Asked on February 15, 2021 by bo-na-fi-de

   

128×64 and pushButtons

1  Asked on February 12, 2021 by iulian-chirvasa

 

ESP32 HTTP Server request interrupts Telnet session

0  Asked on February 1, 2021 by eric-vortriede

       

Power supply for DIY Arduino CNC Router

1  Asked on January 30, 2021 by dragonflyrobotics

     

Size limitation on char Array on ESP8266

0  Asked on January 25, 2021 by guy-d

 

AnalogRead always 1023 on Arduino Due?

6  Asked on January 25, 2021 by littlegator

   

Can’t Upload Sketches to Arduino Nano Every

1  Asked on January 23, 2021 by kronimiciad

       

Storing value in EEPROM in Arduino Uno

2  Asked on January 22, 2021 by jaivishnu

 

When is SPI.beginTransaction required?

3  Asked on January 19, 2021

 

Help with code not working

0  Asked on January 16, 2021 by nc-bean

   

Getting incorrect output from ADC over SPI in Arduino Uno

1  Asked on January 16, 2021 by rithvikp

     

ESP32 start BLE after deep sleep

1  Asked on January 12, 2021 by hgen

       

Not getting input from sensor using timer

0  Asked on January 10, 2021 by noman-gulzar

   

Timer0 overflow interrupt not working

1  Asked on December 29, 2020 by simon-tagne

         

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir