Solved

Templates

Posted on 2000-04-07
14
268 Views
Last Modified: 2010-04-02

 Consider the following template class



template < class KEY, class ARG_KEY, class VALUE, class                ARG_VALUE>
class ClassA:public CObject
{
  protected:
     ClassB *m_elem;
  public :
    ClassA(UINT size)
   {
      m_elem =new ClassB<  KEY,  ARG_KEY, VALUE, ARG_VALUE >;
   }
 
  ............

   (some other functions)

 ...............
};

 Here ,ClassB is another template class which has the same parameters (KEY ,ARG_KEY and so) as ClassA .Unfortunalety it does not compile successfully
did I forgot something ?

Thanks.
 
0
Comment
Question by:e6694811
  • 6
  • 4
  • 4
14 Comments
 
LVL 4

Expert Comment

by:captainkirk
ID: 2693705
what errors are you getting?
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2693746
I tried the following code and it compiled fine:

template < class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
class ClassB:public CObject
{
  protected:
  public :
    ClassB(UINT size) {;}

};



template < class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
class ClassA:public CObject
{
  protected:
     ClassB *m_elem;
  public :
    ClassA(UINT size)
   {
      m_elem =new ClassB<  KEY,  ARG_KEY, VALUE, ARG_VALUE >;
   }
};
0
 

Author Comment

by:e6694811
ID: 2693784

 I forgot to mention that these errors  occur only  
when I try to create a new instance of the class


 This is the line of code where I think I made the mistake:

.....
 ClassA <CPoint ,CPoint ,CPoint ,CPoint>  (123);
.....


  I'm using VC++ 6.0 .Here are the errors:



 :\\ClassA.h(16) : error C2440: '=' : cannot convert from 'class ClassA<class CPoint,class CPoint,class CPoint,class CPoint> *' to 'class ClassB *'


        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


        c:\kk\ClassA.h(14) : while compiling class-template member function '__thiscall ClassA<class CPoint,class CPoint,class CPoint,class CPoint>::ClassA<class CPoint,class CPoint,class CPoint,class CPoint>(unsigned int)'

Let me know if you need additional info.

 REgards.
0
 
LVL 1

Expert Comment

by:tvanceplus
ID: 2693859
instead of declaring the pointer as

ClassB *m_elem;
 

try

ClassB<KEY,ARG_KEY,VALUE,ARG_VALUE>
       * m_elem;


Good Luck!

tvanceplus

0
 

Author Comment

by:e6694811
ID: 2693930
Just one more question  :is the following sentence
rigth ? (It looks a bit strange for me)

m_elem =new ClassB<  KEY,  ARG_KEY, VALUE,        ARG_VALUE >();

 I want to get sure I won't have any problems in the future .On the other hand it already compiles successfully.

 Regards.
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2694054
tvanceplus:

I tried that without success... there is a linkage problem with the constructor when the casting problem is resolved... I'm looking into that now...
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2694077
Here's some code that works:

template <class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
class ClassB : public CObject
{
  protected:
  public :
      ClassB();
      ClassB(UINT size) {;}

};


template <class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
class ClassA : public CObject
{
  protected:
      ClassB* m_elem;
  public :
    ClassA(UINT size)
    {
            m_elem = reinterpret_cast<ClassB*> (new ClassB<KEY, ARG_KEY, VALUE, ARG_VALUE>(size));
    }
};


// declare a var:
ClassA<CPoint, CPoint, CPoint, CPoint> x(123);

let me know if that helps...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:e6694811
ID: 2694102

 Hmm I don't know what to do now.tvanceplus 's
solution seems OK (I do not see any linkage error)

 I want to be fair :tvanceplus 's is there anything
 to say in your "defence" :)
0
 

Author Comment

by:e6694811
ID: 2694103

 Hmm I don't know what to do now.tvanceplus 's
solution seems OK (I do not see any linkage error)

 I want to be fair :tvanceplus 's is there anything
 to say in your "defence" :) ?
0
 
LVL 1

Expert Comment

by:tvanceplus
ID: 2694154
I don't know about your linker problems, but in your original problem that was an error. Depending on your compiler ( some require more type compatibility than others), it will make you specify the type parameters to declare a pointer to a template class.

Captain Kirk's solution should also work fine. However, it is a good practice to declare (if you can) template classes with their type already specified. It is easier to read and recognize the purpose of the object.

I hope that your code works!
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2694670
The problem I got was that the constructor, even with the mods you suggested in place, was flagged as an unresolvable external reference... I didn't mean to imply that your solution caused that problem... I'm using VC++ 6.0...
0
 
LVL 1

Expert Comment

by:tvanceplus
ID: 2694981
You're right.

I missed the fact that ClassB did not have a default constructor.
0
 
LVL 1

Expert Comment

by:tvanceplus
ID: 2701593
tvanceplus changed the proposed answer to a comment
0
 
LVL 4

Accepted Solution

by:
captainkirk earned 40 total points
ID: 2701605
I guess I'll propose the previous submission as an answer...


Here's some code that works:

template <class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
class ClassB : public CObject
{
  protected:
  public :
ClassB();
ClassB(UINT size) {;}

};


template <class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
class ClassA : public CObject
{
  protected:
ClassB* m_elem;
  public :
    ClassA(UINT size)
    {
m_elem = reinterpret_cast<ClassB*> (new ClassB<KEY, ARG_KEY, VALUE, ARG_VALUE>(size));
    }
};


// declare a var:
ClassA<CPoint, CPoint, CPoint, CPoint> x(123);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now