C++ Template, different declaration and definition, linker not able to resolve the symbols [duplicate]

StackOverflow https://stackoverflow.com/questions/21722993

سؤال

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

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top