Solved

Template in RUNTIME_CLASS macro

Posted on 1998-11-16
9
966 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
xyBalance chalenge 58 105
mixString challenge 36 138
string initialization in java 11 122
Excel file not created as expected 7 111
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…
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…
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.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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