G
Guest
This code get's a link error (I'm guessing because the template isn't being
instantiated)...
// For using templates to simplify deleting items
template <class T>
// Returns the next ADHL in the list
AbstractDataHolderList*
// Used to delete this object and the item it points to
AbstractDataHolderList:eleteThisItem(void)
{
// do stuff to update list (since this item is being removed)
delete (typename T*)m_thisItem;
delete this;
return m_nextADHL;
}
/* ... */ main (/* ... */)
{
/*****************************************
* We make some items (CString in this case?)
* and add em to the list, then run the program up here
*****************************************/
// This part is the important thing
while (myADHL != NULL)
myADHL = myADHL->DeleteThisItem<CString>();
return 0;
}
If I change the DeleteThisItem method in the ADHL class to be the
instanciated template for a CString (ie:
"/*...*/:eleteThisItem<CString>(void)"), then there isn't a linking error.
But I'm wondering why the compiler doesn't notice that main is calling the
template with a forced type of CString. Shouldn't the call in main force an
instanciation of the template with T = CString? (as a side note... since
there are no parameters, if <CString> is omited in the DeleteThisItem call,
then this code doesn't even compile, because the type cannot be
determined... which is why i'm wondering, when I say that the type is a
CString, why isn't the CString version of the template created?)
instantiated)...
// For using templates to simplify deleting items
template <class T>
// Returns the next ADHL in the list
AbstractDataHolderList*
// Used to delete this object and the item it points to
AbstractDataHolderList:eleteThisItem(void)
{
// do stuff to update list (since this item is being removed)
delete (typename T*)m_thisItem;
delete this;
return m_nextADHL;
}
/* ... */ main (/* ... */)
{
/*****************************************
* We make some items (CString in this case?)
* and add em to the list, then run the program up here
*****************************************/
// This part is the important thing
while (myADHL != NULL)
myADHL = myADHL->DeleteThisItem<CString>();
return 0;
}
If I change the DeleteThisItem method in the ADHL class to be the
instanciated template for a CString (ie:
"/*...*/:eleteThisItem<CString>(void)"), then there isn't a linking error.
But I'm wondering why the compiler doesn't notice that main is calling the
template with a forced type of CString. Shouldn't the call in main force an
instanciation of the template with T = CString? (as a side note... since
there are no parameters, if <CString> is omited in the DeleteThisItem call,
then this code doesn't even compile, because the type cannot be
determined... which is why i'm wondering, when I say that the type is a
CString, why isn't the CString version of the template created?)