Search code examples
c++templatesc++17typename

Correct use of explicit template instantiation for functions?


I'm currently on an exercise for which I'm supposed to use template matrices as paramters in all my functions and force the needed data types in a header. I don't really know how to do it yet though. There's a header with the following function and a second function with the same syntax (and also the same template name if relevant?):

template <typename multityp> void matsum( multityp **, multityp **, multityp **, int, int );

template void matsum <int> (int**, int**, int**, int, int);
template void matsum <double> (double**, double**, double**, int, int);
template void matsum <bool> (bool**, bool**, bool**, int, int);

When compiling I get the error "variable or field 'matsum' declared void" for "multityp" in the following function which I placed in another header, followed by multiple "variable was not declared" errors:

void matsum ( multityp **matrix1, multityp **matrix2, multityp **ergebnis, int zeilen, int spalten ){
      //Does stuff
}

Solution

  • As per parktomatomi's comment adding

    template <typename multityp>
    

    in front of the body declaration did the trick:

    template <typename multityp> void matsum ( multityp **matrix1, multityp **matrix2, multityp **ergebnis, int zeilen, int spalten ){
          //Does stuff
    }