[SOLVED] C++ function template

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] C++ function template

    Hi everybody!
    Does SES compiler support C++ function templates?
    I made a simple function template like this (for STM32H743VIT6):

    Source Code

    1. template <typename TYPE> TYPE Rescale(TYPE x0, TYPE x1, TYPE y0, TYPE y1);
    2. template <typename TYPE> TYPE Rescale(TYPE x0, TYPE x1, TYPE y0, TYPE y1)
    3. {
    4. TYPE tmp;
    5. return tmp;
    6. }
    and call this function from other function:

    Source Code

    1. auto some_int = Rescale(5, 3, 6, 7);

    But linker says:
    undefined symbol int Rescale<int>(int, int, int, int) referenced by symbol CreateMainWindow() (section .text.CreateMainWindow() in file main_window.o)
    I think I'm doing something wrong.
    Any help would be appreciated!
    THX

    The post was edited 1 time, last by kazham ().

  • First off: a prototype for a function template is almost never necessary or used because any caller of the function template needs to have the complete definition in order to get the particular function template instantiated.

    See isocpp.org/wiki/faq/templates#templates-defn-vs-decl

    So this question is almost certainly not really an SES question but a general C++ question.

    I suspect that you have the prototype in a header that is being included and the definition of the function template in another cpp file. That won't work. Get rid of the prototype and put the definition of the function template in the header.

    If that doesn't solve your problem, then:

    Does this compile and link with gcc/g++ compiler (use mingw if on Windows) targeting the platform you're running it on (ie., not a cross compile) from the command line?

    If so, then it will work in SES.

    If not copy/paste the command line(s) and output (as text, please, not a screenshot). And specify exactly what code is in what file and how the files are #included.