Solved

Template in RUNTIME_CLASS macro

Posted on 1998-11-16
9
959 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 42
Strange date stored 22 120
ODBC Connection Logging, ADO.NET 6 64
Tab names are off by one 5 54
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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