Archived

This topic is now archived and is closed to further replies.

Shannon Barber

Baseclass and templates

Recommended Posts

Shannon Barber    1681
Are you allowed to inherit from a base class with a template? Something like this:
  
class CMsgIDTag
{
public:
CMsgIDTag() : m_MsgID(m_cMsgID++)

protected:
int m_MsgID;

private:
static int m_cMsgID;
};
int CMsgMakerBase::m_cMsgID=0;

//Starts to get funky

template<typename TDest>
class CMsgMakerBase : public CMsgIDTag
{
Initialize(TDest* pDest)
{
_ASSERT(pDest);
m_pDest = pDest;
}
protected:
TDest* pDest;
}
  
I get this error from MSVC when I try
quote:
E:\Projects\DirectShow\LinkIPFilters\LinkIPFilters.h(97) : fatal error C1001: INTERNAL COMPILER ERROR (compiler file ''E:\8966\vc98\p2\src\P2\main.c'', line 494) Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more information Error executing cl.exe.

Share this post


Link to post
Share on other sites
jwalker    122
try changing your template to

  
template <class TDest>


i am not quite sure, but i get alot of template errors
with vc which shouldnt even be errors..

if that cant work .. try this
  
template<typename TDest>
class CMsgIDTag
{

};

template<typename TDest>
class CMsgMakerBase : public CMsgIDTag<TDest>
{

}


{ Stating the obvious never helped any situation !! }

Share this post


Link to post
Share on other sites
Gorg    248
You certainly can do it. MSVC internal error with templates usually happen when there is a syntax error of some sort.

I see a couple of problems :

1. You forgot to give an implementation to the constructor of CMsgIDTag.

2.int CMsgMakerBase::m_cMsgID=0; should be CMsgIDTag::m_cMsgID=0;

3. Initialize does not have a return type.

4. (probably left out this part) there is no ';' to terminated your CMsgMakerBase class



Edited by - Gorg on October 17, 2001 12:04:26 AM

Share this post


Link to post
Share on other sites
Shannon Barber    1681
Yeah, come to think of it, that last time I got a compiler error, I think I had an extra > somewhere.

That''s not the exact code, as the exact code is about two pages... if I can''t figure it out tomorrow, I''ll give a full example. I just wanted to make sure I wasn''t breaking a rule I didn''t know about.

And I''m pretty sure you have to give the type when you initialize a static property.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I think MSVC++ is just generally really crappy when it comes to handling templates. I often get Internal Compiler Errors when using the STL. Sometimes inserting whitespace or comments, or just moving a couple of (completely template-unrelated) lines solves the problem (don''t ask me how ).

e.g. the following code (in a class decl) gave me an Internal Compiler Error
  
typedef std::pair< std::string, std::string > StringPair;
typedef std::map< StringPair, std::string > PropertyMap;
PropertyMap properties;


But after inserting a comment it worked:
  
typedef std::pair< std::string, std::string > StringPair;
typedef std::map< StringPair, std::string > PropertyMap;
/* foo */
PropertyMap properties;

I can''t for the life of me figure out what was wrong.

Share this post


Link to post
Share on other sites
Gorg    248
quote:

And I''m pretty sure you have to give the type when you initialize a static property.


My mistake, yes you need to include the type but you have the wrong class.

You used CMsgMakerBase, but your static object is in CMsgIDTag, so you need to write

int CMsgIDTag::m_cMsgID=0;

not

int CMsgMakerBase::m_cMsgID=0;

By the way, I made your little code snipplet compile by fixing the errors I mentionned.

Share this post


Link to post
Share on other sites
Shannon Barber    1681
I used an _ASSERT on a method pointer, and that is apparently the source on the compiler exception.

    
typedef HRESULT (TDest::*MessageProc)(Message*);
virtual HRESULT Initialize(TDest* pDest, MessageProc pProc, IMsgParser* pParser, IMalloc* pMalloc)
{
_ASSERT(pDest);
_ASSERT(pProc);
_ASSERT(pMalloc);
m_pDest = pDest;
m_pProc = pProc;
m_spMalloc = pMalloc;
return pParser->RegisterMaker(this);
}


Changing _ASSERT(pProc); to _ASSERT(pProc!=0); fixed the problem.

Edited by - Magmai Kai Holmlor on October 18, 2001 3:38:27 PM

Share this post


Link to post
Share on other sites