I wanted to write my library, I started with a template array output function. Compiler swears: candidate template ignored: could not match const type-parameter-0-0 * 'against' char ' Where is the error?

And another question, if I need a library to be located in one directory and main.cpp in another, then I connect SMKLibrary.h to main.cpp with the address of the library directory. I compile main.cpp like this: g ++ * .cpp && a.out but I get an error. It works when all the files in the same directory, when in different is not. What should I write in the terminal to compile from two different directories at once?

main.cpp

#include <iostream> #include "SMKLibrary.h" int main() { char a[5] = {"ASFD"}; array_print(a,5); return 0; } 

SMKLibrary.h

 #ifndef SMKLIBRARY_H #define SMKLIBRARY_H #include <iostream> template <typename T> void array_print(const T * array[], int size); #endif 

SMKLibrary.cpp

 #include "SMKLibrary.h" template <typename T> void array_print(const T * array[], int size) { int last = size - 1; for (int i = 0; i < last; i++) { std::cout << array[i] << " "; } std::cout << array[last] << std::endl; } 

    1 answer 1

    1. So far, the compiler should see the templates in full, with the body, and not just prototypes. So transfer the definition of the template function to the .h file.

    2. In the command line, specify not the *.cpp mask, but all the files that you need to compile.

    3. Replace

    void array_print(const T* array[], int size)

    on

    void array_print(const T array[], int size)

    or at

    void array_print(const T * array, int size)

    • Transferred the function body to the header. @Harry Same error error: note: candidate template ignored: couldn’t match const type-parameter-0-0 * 'against' char ' - Volodymyr Samoilenko
    • @VladimirSamoilenko Here is your code after completing 1 and 3 points of my answer (I just transferred the template directly to the main file) - ideone.com/0t0OTf - do you have the same? Compiled? or gives an error? - Harry
    • Everything, I understood what you mean. Thank you - Volodymyr Samoilenko