Class template argment list
-
NOTE: In the following text and code I use [] rather than the greater than and less than signs because of the HTML editor. I read about class templates in three books and MSDN and thought I understood. So, I created a SDI test project in VC++ to learn about them. I added only this single CTestTemp class. As you can see, I didn't get far. The following code keeps giving me a C2955 (use of class template requires template argument list) error in TestTemp.cpp each place a function is defined (or BEGIN MESSAGE MAP). I have tried all kinds of argument lists in the constructor function, tried putting template[class T] in various places in the code, but nothing works. If I add a new function, the same error flags the new function. It appears I'm doing somehting wrong in the function definition. I'm at such a basic point, I figure I must be doing somehting equally basic wrong. Ideas? Thanks. //TestTemp.h template[class T] <--only thing I add class CTestTemp : public CWnd { public: CTestTemp(); virtual ~CTestTemp(); protected: DECLARE_MESSAGE_MAP() }; //TestTemp.cpp CTestTemp::CTestTemp() <---error C2955 { } CTestTemp::~CTestTemp() <---error C2955 { } BEGIN_MESSAGE_MAP(CTestTemp, CWnd) <---error C2955 //{{AFX_MSG_MAP(CTestTemp) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP()
-
NOTE: In the following text and code I use [] rather than the greater than and less than signs because of the HTML editor. I read about class templates in three books and MSDN and thought I understood. So, I created a SDI test project in VC++ to learn about them. I added only this single CTestTemp class. As you can see, I didn't get far. The following code keeps giving me a C2955 (use of class template requires template argument list) error in TestTemp.cpp each place a function is defined (or BEGIN MESSAGE MAP). I have tried all kinds of argument lists in the constructor function, tried putting template[class T] in various places in the code, but nothing works. If I add a new function, the same error flags the new function. It appears I'm doing somehting wrong in the function definition. I'm at such a basic point, I figure I must be doing somehting equally basic wrong. Ideas? Thanks. //TestTemp.h template[class T] <--only thing I add class CTestTemp : public CWnd { public: CTestTemp(); virtual ~CTestTemp(); protected: DECLARE_MESSAGE_MAP() }; //TestTemp.cpp CTestTemp::CTestTemp() <---error C2955 { } CTestTemp::~CTestTemp() <---error C2955 { } BEGIN_MESSAGE_MAP(CTestTemp, CWnd) <---error C2955 //{{AFX_MSG_MAP(CTestTemp) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP()
you need, at least, to put 'T' into your constructor:
CTestTemp(T t);
without it, you have no way to tell the compiler what type to use for T -
NOTE: In the following text and code I use [] rather than the greater than and less than signs because of the HTML editor. I read about class templates in three books and MSDN and thought I understood. So, I created a SDI test project in VC++ to learn about them. I added only this single CTestTemp class. As you can see, I didn't get far. The following code keeps giving me a C2955 (use of class template requires template argument list) error in TestTemp.cpp each place a function is defined (or BEGIN MESSAGE MAP). I have tried all kinds of argument lists in the constructor function, tried putting template[class T] in various places in the code, but nothing works. If I add a new function, the same error flags the new function. It appears I'm doing somehting wrong in the function definition. I'm at such a basic point, I figure I must be doing somehting equally basic wrong. Ideas? Thanks. //TestTemp.h template[class T] <--only thing I add class CTestTemp : public CWnd { public: CTestTemp(); virtual ~CTestTemp(); protected: DECLARE_MESSAGE_MAP() }; //TestTemp.cpp CTestTemp::CTestTemp() <---error C2955 { } CTestTemp::~CTestTemp() <---error C2955 { } BEGIN_MESSAGE_MAP(CTestTemp, CWnd) <---error C2955 //{{AFX_MSG_MAP(CTestTemp) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP()
Fixing the constructor/destructor syntax is one thing. I'm not sure how you're going to get a template class through the CWnd message map macros though...
//TestTemp.h
template <class T>
class CTestTemp : public CWnd
{
public:
CTestTemp();
virtual ~CTestTemp();protected:
DECLARE_MESSAGE_MAP()
};//TestTemp.cpp
template <class T> CTestTemp<T>::CTestTemp()
{
}
template <class T> CTestTemp<T>::~CTestTemp()
{
}// I don't know how to make this next part work!
BEGIN_MESSAGE_MAP(CTestTemp<int>, CWnd)
//{{AFX_MSG_MAP(CTestTemp)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
NOTE: In the following text and code I use [] rather than the greater than and less than signs because of the HTML editor. I read about class templates in three books and MSDN and thought I understood. So, I created a SDI test project in VC++ to learn about them. I added only this single CTestTemp class. As you can see, I didn't get far. The following code keeps giving me a C2955 (use of class template requires template argument list) error in TestTemp.cpp each place a function is defined (or BEGIN MESSAGE MAP). I have tried all kinds of argument lists in the constructor function, tried putting template[class T] in various places in the code, but nothing works. If I add a new function, the same error flags the new function. It appears I'm doing somehting wrong in the function definition. I'm at such a basic point, I figure I must be doing somehting equally basic wrong. Ideas? Thanks. //TestTemp.h template[class T] <--only thing I add class CTestTemp : public CWnd { public: CTestTemp(); virtual ~CTestTemp(); protected: DECLARE_MESSAGE_MAP() }; //TestTemp.cpp CTestTemp::CTestTemp() <---error C2955 { } CTestTemp::~CTestTemp() <---error C2955 { } BEGIN_MESSAGE_MAP(CTestTemp, CWnd) <---error C2955 //{{AFX_MSG_MAP(CTestTemp) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP()
The problem with templates is that you can't have the definition in a .h and the implementation in .cpp Templates are like macros in that they are processed before the compilaton proper. There is a reason that I can't remember why that causes the problem... something to do with operating at the file scope rather than program scope There are a few ways around it, one is to write everything into the class defintion in the .h file. Contrary to popular belief this will not stop your code from working, but it is generally not good practice. There are some more elaborate workarounds out there if you look on Google... Hope this helps.