0

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);
}
11
  • put the template function definition in the .h file.
    – Juraj
    Jul 24, 2020 at 5:40
  • @Juraj when leaving the template defining inside .h file only I get :error: variable or field 'funcOne' declared void void myTest::funcOne(T1 arg)
    – guyd
    Jul 24, 2020 at 6:33
  • Please provide a minimal complete example. The error might be in the calling/using source. Jul 24, 2020 at 6:52
  • @thebusybee Since it is only example - I've put myTest class inside another .cpp , and .hfiles I'm currently using. funcOne is called during setup() function
    – guyd
    Jul 24, 2020 at 6:59
  • Would you mind to show us the commands to compile and link? Oh, and the source of this other module, please. You can enable verbose output of the build in the preferences. Jul 24, 2020 at 7:03

1 Answer 1

2

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);
    }
};
10
  • Can you please explain what does it differ from what I originaly wrote in my question ?
    – guyd
    Jul 24, 2020 at 7:17
  • 1
    the template is a declaration. the source where you use the template can't see it in cpp
    – Juraj
    Jul 24, 2020 at 7:18
  • Actually duplicate the function insde .h file ?
    – guyd
    Jul 24, 2020 at 7:18
  • 1
    I write "move". It should be only in .h
    – Juraj
    Jul 24, 2020 at 7:18
  • Compilers don't know which class to use for the templated class if you compile the method separately. The common way is to put the method implementation into the header so the compiler is able to instantiate the specific implementation. Jul 24, 2020 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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