-1

Doing simple template code given here; just that I am writing separate declaration and definition

This is my header file

template <typename T>
class SmartPointerGen
{
private:
        T*  pData;

public:
        SmartPointerGen(T* pValue);
        ~SmartPointerGen();

        T&  operator* ();
        T*  operator-> ();
};

This is how I define the methods

    #include "SmartPointer_Generic.h"

    template <typename T> 
    SmartPointerGen<T>::SmartPointerGen(T* pValue) :    pData(pValue)
    {

    }

    template <typename T>
    SmartPointerGen<T>::~SmartPointerGen()
    {
        delete pData;
    }

    template <typename T>
    T&  SmartPointerGen<T>::operator* ()
    {
        return *pData;
    }

    template <typename T>
    T*  SmartPointerGen<T>::operator-> ()
    {
        return pData;
    }

And in main(), I try to use the same with

SmartPointerGen<Person> pPersonGen(new Person("WTF Again"));

I believe the problem is with the definition of the SmartPointerGen class, I tried following this syntax for defining templatized methods outside class, but the linker is saying unresolved symbols for destructor and overloaded operators, (BUT NOT FOR THE CONSTRUCTOR).

errors:(showing only 1, others are quite similar)

main.obj : error LNK2019: unresolved external symbol "public: __thiscall SmartPointerGen<class Person>::SmartPointerGen<class Person>(class Person *)" (??0?$SmartPointerGen@VPerson@@@@QAE@PAVPerson@@@Z) referenced in function _main

1 Answers1

2

You have to define templates in the same header file they were declared - it's language restriction.

Arsenii Fomin
  • 3,120
  • 3
  • 22
  • 42
  • means..? did not exactly get you.. I mean can't i write class in header file and method def. in some other file? Is so, is this because of two stage compilation required for templates? –  Feb 12 '14 at 09:03
  • 1
    means: all code should go in the header file, no CPP for templates (unless you include CPP files too) – Ferenc Deak Feb 12 '14 at 09:03
  • yep, all template code should be in header file. – Arsenii Fomin Feb 12 '14 at 09:04
  • 1
    Not necessarily in the same file. It can be anywhere, as long as the definition is visible in every file which uses the template. – Angew is no longer proud of SO Feb 12 '14 at 09:09
  • 1
    Being this the answer I agree with, if you know in advance which datatypes will use the templates (that is - this is not a library), you can look at [this SO question and answers](http://stackoverflow.com/questions/21481864) – Sigi Feb 12 '14 at 09:10
  • Reason is that template specializations are generated on demand. For example, you have vector container. If there is no vectors in your program there is no any template code generated. If you have vector, int specialization of vector would be generated. It becomes very important if you write a library. You can't store all possible template specialization code in library object files (it's infinite) on the one hand. On the other hand you already should have all object code to provide your library users. It's paradox. – Arsenii Fomin Feb 12 '14 at 09:14