0

i have following 2 structs which are defined within a template class and a container, which holds elements of the base class, as follows:

class template<typename T1, typename T2>
class TTestDataObject
{
private:
//base element
struct SDataContainerElement
  {
    T1* m_sData;
  };
//derived element
struct SInvalidDataContainerElement : SDataContainerElement
  {
    int m_eExpectedErrorCode;
  };

//container holding base elements
typedef std::map<T2, SDataContainerElement* > TDataContainer;

TDataContainer sCInvalidData;

public:
typedef TDataContainer::const_iterator TDataConstIterator;
}

I want to implement a method, which can extract information from this container, doing different things depending on which element it has, the base class (SDataContainerElement) or the derived class (SInvalidDataContainerElement) and I implemented it as follows :

template<typename TDataStruct, typename TDataEnum>
int TTestDataObject<T1, T2>::eGetExpectedError(T2 eIndex)
{

  TDataConstIterator sElement = sCInvalidData.find(eIndex);

 if(dynamic_cast<SInvalidDataContainerElement*>((sElement->second)) == NULL)
   return -1;
 else    
   return static_cast<int>(sElement->second->m_eExpectedError);
}

Trying to compile leads to the following error :

E2307 Type'TTestDataObject<BEREICHTYP,eTestDataBereichTyp>::SDataContainerElement' is not a defined class with virtual functions

I don't understand this. Can anybody explain this error to me and show me a solution pls?

Thanks in advance!

4

1 回答 1

0

As described in FAQ: Why does dynamic_cast only work if a class has at least 1 virtual method? , i need a virtual method, like a virtual destructor, in the base class.

To make this code work, i simply added

virtual ~SDataContainerElement(){};

to the base - struct. Thanks!

于 2013-06-27T10:06:38.493 回答