Monday, January 29, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...