0

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
}
Mareya
  • 11
  • 3
  • Could you provide a complete reproducible example? Hardly to find the problem from only those code snippets. – Daniel Langr Nov 21 '20 at 16:38
  • Looking at this answer, it might be that `multityp` is not declared: https://stackoverflow.com/questions/364209/variable-or-field-declared-void . Given how `multityp` is used above, are you missing `template ` before `void matsum`? – parktomatomi Nov 21 '20 at 16:47
  • @DanielLangr This is really all code for this function, all other code relates to other functions that are structually exact copies of this one. Only thing to add would be the guardian for the header with #ifndef [...] so typing this into a .hpp and compiling should give the errors provided. – Mareya Nov 21 '20 at 19:57
  • @parktomatomi Well now I feel kinda stupid... I didn't really think of the `template ` as part of the function head so I left it out for defining the body. Now it works! – Mareya Nov 21 '20 at 20:02

1 Answers1

0

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
}
Mareya
  • 11
  • 3