Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

class def

Posted on 2000-02-14
11
Medium Priority
?
281 Views
Last Modified: 2010-04-02
I receive an error when I try this:

template <typename HANDLE _Init=NULL>
class CMyClass
{
public:
        CMyClass()
        {
                m_hHanlde = _Init;
        }
protected:
        HANDLE m_hHandle;
};

Now to define a specility of the class:

typedef CMyClass<-1> CMyOtherClass;

This is where the compiler gives an error: a signed int cannot be converted/casted to a void* (which is a HANDLE)

typedef CMyClass<0xFFFFFFFF> CMyOtherClass;

Doesn't work either !

Anybody knows the answer ?

grtx, RoverM
0
Comment
Question by:roverm
[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
  • 7
  • 3
11 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2521707
In short, non-type template parameters must either be some form of const expression or a pointer/reference to an object with external linkage.

You would need a declaration:
  extern HANDLE MyHandle;
a definition
  HANDLE MyHandle = -1;

Parameters for your template must always heave external linkage:

extern HANDLE hGl;
void f()
{
   HANDLE hLc;
   CMyClass<hLc> cm1; // error
   CMyClass<hGl> cm2; // ok

   //....  
}
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2521722
More precisely (from '96 Draft):
==========================================
3 A non-type template-parameter shall have one of the following (option-
  ally cv-qualified) types:

  --integral type, accepting an integral constant expression as an argu-
    ment,

  --enumeration  type,  accepting  an integral constant expression as an
    argument,

  --pointer to object, accepting an address constant  expression  desig-
    nating a named object with external linkage,

  --reference  to  object,  accepting an lvalue expression designating a
    named object with external linkage,

  --pointer to function, accepting an  expression  of  type  pointer  to
    function designating a function with external linkage,

  --reference  to function, accepting an lvalue expression designating a
    function with external linkage,

  --pointer to member, accepting an address constant  expression  desig-
    nating a named member of a class.
==========================================
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2523019
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2523321
Kangaroo, the parameter that roverm uses IS a constant expression, of course it must be a parameter that is known during compile-time, but that is the case in his example. So that is not the source of the problem.
Try to cast it explicitly to HANDLE (void*) this will solve you're problem.
Luc
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2523456
HANDLE is not any of the allowed types (see above). A HANDLE is typedef-ed as a void*, so only void* (HANDLE's) with external linkage can be used as parameter for the template.
Explicit casting to HANDLE will not solve the problem.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2523469
>> HANDLE is not any of the allowed types
That is, it is allowed if it has external linkage, as in my example.
0
 
LVL 12

Author Comment

by:roverm
ID: 2530226
Sorry, still get errorsssssss!
like 'invalid template error'
0
 
LVL 12

Author Comment

by:roverm
ID: 2530228
Further, I'm trying to instanciate a new definition using TypeDef, NOT by using instanciating a class !

RoverM
0
 
LVL 7

Accepted Solution

by:
KangaRoo earned 200 total points
ID: 2530305
True. A non-type argument of pointer type must be a pointer to an object with external linkage.

#include <windows.h>

template<HANDLE* h>
class MyClass {};

extern HANDLE myhandle;

typedef MyClass<&myhandle> Something;

void f()
{
   MyClass<&myhandle> mc;
}
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2534448
>> I'm trying to instanciate a new definition using TypeDef
You can not instantiate a tmplate specialization with a typedef, if that is what you are trying to do.
0
 
LVL 12

Author Comment

by:roverm
ID: 2549912
Kangaroo:
You're right! It cannot be done!
So I'll give you the points!

D'Mzzl!
RoverM
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

604 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