Solved

Template in RUNTIME_CLASS macro

Posted on 1998-11-16
9
961 Views
Last Modified: 2013-11-20
I've declared a template class which looks like this:

template <class INPUT_WND_TYPE>
CInpWnd<INPUT_WND_TYPE>::CInpWnd() : INPUT_WND_TYPE()
{
...
}

Then later, I pass a class name to the RUNTIME_CLASS macro like this:

RUNTIME_CLASS(CInpWnd<CSaisieView>)

When I compile, I get this error message:

C:\Windows\Bureau\Saisie\Saisie.cpp(78) : error C2039: 'classCInpWnd' : is not a member of 'CInpWnd<class CSaisieView>'

It seems that a parameter of type 'aaa<bbb>' passed to a macro has only the 'aaa' part being read.

Can someone help me with this.  The use of templates for this situation in my program makes my code much smaller and more generic.
0
Comment
Question by:jhendrix
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 1324775
Did you provide the DECLARE_DYNCREATE()/IMPLEMENT_DYNCREATE() macros in your temlate header file? The error you're getting usually is a result when omitting these...
0
 

Author Comment

by:jhendrix
ID: 1324776
Comment for JKR:

I cannot use these macros and I hope to be clear on this cause I might not be able to explain this well.

Here is my template class:

     template <class INPUT_WND_TYPE>
     CInpWnd<INPUT_WND_TYPE>::CInpWnd() : INPUT_WND_TYPE()
     {    
     ...
     }

I cannot use DECLARE_DYNCREATE(CInpWnd) here cause CInpWnd is not a class and will never be one.  CInpWnd<CMyDialog> and CInpWnd<CMyView> will be classes for example that's when the INPUT_WND_TYPE part of the template will be resolved.
CInpWnd<INPUT_WND_TYPE> is a template class which derives itself from class  INPUT_WND_TYPE, which will be any MFC class derived from CObject.  

Now in the InitInstance() function of my program, the AppWizard inserts:

      pDocTemplate = new CSingleDocTemplate(
            IDR_MAINFRAME,
            RUNTIME_CLASS(CMyDoc),
            RUNTIME_CLASS(CMainFrame),       // main SDI frame window
//            RUNTIME_CLASS(CMyView)
// I replaced the above line by this next line for the purpose of my program
            RUNTIME_CLASS(CInpWnd<CMyView>)
            );
      AddDocTemplate(pDocTemplate);

The compiler here then takes the macro parameter 'CInpWnd<CMyView>' and truncates the '<CMyView>' part.  This is one part of my problem.

The other and more general part is how do I use the xxx_DYNCREATE stuff on CInpWnd<CMyDiaog> and CInpWnd<CMyView>.  Where and how can I use these 3 lines:

DECLARE_DYNAMIC(CInpWnd<CMyView>)
IMPLEMENT_DYNAMIC(CInpWnd<CMyView>)
RUNTIME_CLASS(CInpWnd<CMyView>)

0
 

Author Comment

by:jhendrix
ID: 1324777
Adjusted points to 100
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 1324778
Well, the first problem is that (in order to use the 'RUNTIME_CLASS()' macros) your class must be derived from 'CObject' (otherwise the instantiation of 'CSingleDocTempate
' will fail anyway...) - which a template class can't be (by nature). The macros i mentioned expand to (e.g., in your case)
protected:
      static CRuntimeClass* PASCAL _GetBaseClass();
public:
      static const AFX_DATA CRuntimeClass classCInpWnd<CMyView>; // <-- this one is missing
      virtual CRuntimeClass* GetRuntimeClass() const;

//.cpp
      const AFX_DATADEF CRuntimeClass class_name::classCInpWnd<CMyView> = {
            CInpWnd<CMyView>, sizeof(class CInpWnd<CMyView>), wSchema, pfnNew,
                  RUNTIME_CLASS(base_class_name), NULL };
      CRuntimeClass* CInpWnd<CMyView>::GetRuntimeClass() const
            { return RUNTIME_CLASS(class_name); }

where CRuntimeClass is mainly a structure holding some administrative data... (phew! <s>)
0
 
LVL 3

Expert Comment

by:GGRUNDY
ID: 1324779
Macro expansion happens before template expansion and
The DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros on which RUNTIME_CLASS depend upon the
macro concat ## operation. This makes things difficult.

I reckon the best work-a-round for your problem is to
wrap your template definition in a MACRO which will
handle the DECLARE_DYNAMIC/IMPLEMENT_DYNAMIC
macros for you.
e.g.
//-------------------- your template -------------------
//-------------------- or something similar ---------
template <class INPUT_WND_TYPE>
class CInpWnd: public INPUT_WND_TYPE
{
  public:
  void OnDraw(class CDC *);
  };

//------------------ the wrapping macros -------------
//------------------- in a header ----------------
#define DECLARE_TEMPLATE_DYNAMIC(template_name, base_class_name) \
class template_name##base_class_name : public template_name<base_class_name> \
{ \
  DECLARE_DYNAMIC(template_name##base_class_name); \
  }

#define IMPLEMENT_TEMPLATE_DYNAMIC(template_name, base_class_name) \
  IMPLEMENT_DYNAMIC(template_name##base_class_name, base_class_name);

//----------------- call the macros
//----------------- in your c code somewhere -------------
DECLARE_TEMPLATE_DYNAMIC(CInpWnd, CView);
IMPLEMENT_TEMPLATE_DYNAMIC(CInpWnd, CView);

//----------------- and then you can declare instances
//----- NOTE not CInpWnd<CView > but CInpWndCWiew

CInpWndCView view;

//---------------------------------
Cheers Greg

void f()
{
  RUNTIME_CLASS(CInpWndCView);
  }

0
 
LVL 86

Expert Comment

by:jkr
ID: 1324780
Greg - the problem is that in order to get dynamic creation working the classes that use it will have to be derived from CObject - expanding the macros isn't difficult at all (simply add '/P' to the compiler switches to see the preprocessed output)
0
 

Author Comment

by:jhendrix
ID: 1324781
Thanx GGRUNDY,

I always like simple solutions that work, although I'm kinda disappointed in myself for not being able to come up with it.  I'll be going to sleep a bit smarter tonight.
This actually solved another problem I was probably getting because of the way I was trying to do this.

I don't know how this experts-exchange stuff works but why didn't you use the 'answer' button for giving me the solution?  There's 100 points to be had.
0
 
LVL 3

Accepted Solution

by:
GGRUNDY earned 100 total points
ID: 1324782
Answer as above
0
 

Author Comment

by:jhendrix
ID: 1324783
Thanx GGRUNDY,

I always like simple solutions that work, although I'm kinda disappointed in myself for not being able to come up with it.  I'll be going to sleep a bit smarter tonight.
This actually solved another problem I was probably getting because of the way I was trying to do this.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 92
Folder Comparison 12 54
Excel Use VBA to get user's Mac Address for their computer 5 242
fix34  challenge 9 141
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question