@huxley Got a question for you (and any other C++ furs out there)

I've got a template function, and I'm calling it from main(). This works fine, however when I move formatArray() into print.cpp and add a matching prototype to print.h, it can no longer find the function. I can put other functions in print.h and the compiler finds them, there's just something odd about including a header with a template function I'm not getting.

@maldrasen @huxley this is a problem with templates and .cpp files. The template generally needs to be defined in a header, because the compiler only makes an instantiation of it for a particular type when it actually gets used. So if the template is defined in the printf.cpp but it’s used in main.cpp, then the compiler doesn’t see that use case when it compiles printf.cpp and thus no instance of the function gets made.

Basically, put template definitions in a header.

@huxley @maldrasen your other option is explicit instantiation - see https://learn.microsoft.com/en-us/cpp/cpp/source-code-organization-cpp-templates?view=msvc-170 for the skinny on that.
Source code organization (C++ Templates)

Learn more about: Source code organization (C++ Templates)

@19 @huxley @frang

Ahh, got it thanks. When you think of the template as working, um, like a template it makes sense.

Coming from Java I was thinking that they worked like generics, and .h files are like interfaces, but no. Not at all.

Must first unlearn to learn.

@maldrasen @19 @frang there's a lot of stuff about c++ that came about because of the need for backwards compatibility. It's an old, ugly language and has a lot of gotchas
@huxley @maldrasen @frang We need to send Marie Kondo to the C++ Standards Committee.
@huxley @maldrasen @frang One thing I try to communicate to learners who feel bewildered is that this sort of gotcha was not a design choice of the language. It wasn’t intended that we’d have to put all our implementations in headers. It’s a consequence of *other* choices whose problems only became obvious after it was too late. And then other language features got bolted on in an attempt to fix it, then more features trying to fix problems the last fix caused, and here we are.
@19 @maldrasen @frang it's amazing c++ works as well as it does, or that it's been able to stay relevant. Predictions of its demise have never panned out