By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,922 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,922 IT Pros & Developers. It's quick & easy.

forward declaration and templated class

P: n/a
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
Jul 31 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Vince wrote:
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

Jul 31 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.