forward declaration and templated class

V

Vince

Hi,

I have a templated class implemented in one file called CDynWnd.h
and declared like this :


CDynWnd.h (declaration and implementation)
------------

template <class BASECLASS> class CDynWnd : public BASECLASS
{

// Construction
public:
CDynWnd(int nIDD);
CDynWnd(int nIDD, CWnd* pParent);
CDynWnd();

... and so on ...
}


Now I would like to declare a new class called CDynContextMenu,
so in the .h I declare it as shown below with a forward declaration

DynContextMenu.h
-----------------
#include "DynContextMenu.h" (Only declaration)
template <class BASECLASS> class CDynWnd;
class CDynContextMenu
{

public:
CDynContextMenu();
virtual ~CDynContextMenu();

virtual CMenu* GetPopupMenu( CDynWnd<BASECLASS>* editor );

protected:
CMenu m_menu;

};

DynContextMenu.cpp (implementation)
----------------
....
....





But when I compile I get this :

Compiling...
DynContextMenu.cpp
d:\developpement\borne_300705\dyncontextmenu.h(13) : error C2065:
'BASECLASS' : undeclared identifier
 
J

Jonathan Mcdougall

Vince said:
Hi,

I have a templated class implemented in one file called CDynWnd.h
and declared like this :


CDynWnd.h (declaration and implementation)
------------

template <class BASECLASS> class CDynWnd : public BASECLASS
{

// Construction
public:
CDynWnd(int nIDD);
CDynWnd(int nIDD, CWnd* pParent);
CDynWnd();

... and so on ...
}


Now I would like to declare a new class called CDynContextMenu,
so in the .h I declare it as shown below with a forward declaration

DynContextMenu.h
-----------------
#include "DynContextMenu.h" (Only declaration)
template <class BASECLASS> class CDynWnd;
class CDynContextMenu
{

public:
CDynContextMenu();
virtual ~CDynContextMenu();

virtual CMenu* GetPopupMenu( CDynWnd<BASECLASS>* editor );

The name BASECLASS does not exists by itself. It is a template
parameter which must be obtained. You do that by making the class a
template class or (probably what you want) by making this member
function a template:

template <class BASECLASS>
CMenu *GetPopupMenu(CDynWnd<BASECLASS> *editor)
{
}

Two implications: GetPopupMenu() cannot be virtual anymore and you will
probably need to define the function inline.

Think about it: you want to pass a type to CDynWnd, but you don't have
that type. If you wanted for example and int:

CMenu *GetPopupMenu(CDynWnd<int> *editor)
{
}

but since you want any type, you need to use a template, hence

template <class BASECLASS>
CMenu *GetPopupMenu(CDynWnd<BASECLASS> *editor)
{
}

Note that you could use any name:

template <class T>
CMenu *GetPopupMenu(CDynWnd<T> *editor)
{
}
protected:
CMenu m_menu;

};


But when I compile I get this :

Compiling...
DynContextMenu.cpp
d:\developpement\borne_300705\dyncontextmenu.h(13) : error C2065:
'BASECLASS' : undeclared identifier

You should read a bit more about templates before continuing. It seems
there are some basic notions you don't get.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top