0

I am trying to organize files while using function templates as shown in the following:

template <class T>
T max(T a, T b)
{
    return a > b ? a : b;
}

int main()
{
    cout << "max(10, 15) = " << max(10, 15) << endl;

    retun 0;

}

I simply put the implementation of "max(T a, T b)" in a templateMaxFun.cpp and the following in templateMaxFun.h file:

template <class T>
T max(T a, T b);

Then, I put #include "templateMaxFun.h" in the main.cpp. However, the compiler is complaining: "undefined reference to `int max(int, int)".

My question is: what is the correct way to put a template function prototype in the .h file? I am guessing my problem might be here.

Thanks for your help.

  • "the compiler is complaining: "undefined reference to `int max(int, int)"." - That's not the *compiler* complaining. That's the *linker* complaining. – Jesper Juhl Nov 10 '18 at 20:22
  • Yes, it is a duplicated question. Thanks for pointing out the right place for the answers. – Spring19981 Nov 10 '18 at 21:20

0 Answers0