Monday, 29 January 2018

c++ - Using Template Member data

I was trying to use a template based class as a member of another class. This other class will decide based on its data member value what is the data type that the template based member should use. To this end I used bit of polymorphism to decide on run time the instantiation.



class Base
{
public:
virtual void print() = 0;

};

template
class templateDynamic : public Base
{
public:
templateDynamic();
~templateDynamic();
void print();
};


template
templateDynamic::templateDynamic()
{
}

template
templateDynamic::~templateDynamic()
{
}


template
void templateDynamic::print()
{

}

class Holder
{
private:

Base * m_ABC;
public:
Holder(int a);
void print();
};

void Holder::print()
{
m_ABC->print();
}


Holder::Holder(int a)
{
if(a == 1)
m_ABC = new templateDynamic();
else
m_ABC = new templateDynamic();
}

int main()

{
Holder aHolder(1);
aHolder.print();
Holder aHolder2(2);
aHolder.print();
}


Print function will then print based on the type of T is it an int or float.
I am getting a linker error at this point.





error LNK2019: unresolved external symbol "public: __thiscall
templateDynamic::templateDynamic(void)"
(??0?$templateDynamic@H@@QAE@XZ) referenced in function "public:
__thiscall Holder::Holder(void)" (??0Holder@@QAE@XZ)


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...