c++ class template (not inline)

I have coded a C++ class template that is a descendent of the STL map template. The code compiles and runs as expected. I have coded it inline in a header file. How do I code the member functions in a separate cpp file, using the header file for the prototype declaration only ?
The code in the header file is a follows :

#include <map.h>

template<class T1,class T2>
class MyMap : public map<T1,T2>
    map<T1,T2> &mx;
    T2 *x;
    MyMap()  : mx(*this){};
    map<T1,T2> operator = (map<T1,T2>  m)
        mx = m;
        return m;
    T2& operator[](T1 k)
            x = &new T1();
            mx[k]= *x;
        return  mx[k];
Who is Participating?
phoffricConnect With a Mentor Commented:
Here is a  more detailed elaboration of why you don't separate the template body code from the template declarations.
Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?

But, if you really must, there is a way that requires extra maintenance when you need to add a new static instantiation of your template class. Look at the next few FAQ section:
"How can I avoid linker errors with my template functions?" as well as
"How can I avoid linker errors with my template classes?".

The message from this last link is that in your .cpp file, you will need to add as many specific static instantiations as you  program requires - something like:
  template class MyMap <int, int>;
  template class MyMap <string, int>;

When you compile this .cpp file, you get the body of all the different signatures your program needs. If you later need a <string, string>, then you have to go back and add
    template class MyMap <string, string>;
to the template's .cpp file.
Normally, you do not code the template member functions in a separate cpp file since the templates are not actual code that is run until the compiler statically instantiates them. If another module uses your header and tries to instantiate them, it will not know how since it doesn't have direct access to the template .cpp file.

Is there a reason why you want to separate them?
jkrConnect With a Mentor Commented:
>>How do I code the member functions in a separate cpp file, using the header file for the
>>prototype declaration only ?

Just like with any other class that would not be templated. The caveat just is: While the C++ Standard technically allows doing that, most compilers don't support it. So, if that already works for you, the question basically is: Why putting in the effort if chances are that you will have to add a '#include "MyMap.cpp"' to get it to work anyway?
alcindorAuthor Commented:
Thanks for your comments. I have a better understanding now and will not be coding the definition from the declaration.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.