forward declaration and templated class



 DEVELOP > c-Plus-Plus > forward declaration and templated class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Vince"
Date: 31 Jul 2005 03:48:54 AM
Object: forward declaration and templated class
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
.

User: "Jonathan Mcdougall"

Title: Re: forward declaration and templated class 31 Jul 2005 04:08:05 AM
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER