Link to home
Start Free TrialLog in
Avatar of e6694811
e6694811

asked on

Templates


 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.
 
Avatar of captainkirk
captainkirk

what errors are you getting?
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 >;
   }
};
Avatar of e6694811

ASKER


 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.
instead of declaring the pointer as

ClassB *m_elem;
 

try

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


Good Luck!

tvanceplus

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.
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...
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...

 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" :)

 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" :) ?
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!
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...
You're right.

I missed the fact that ClassB did not have a default constructor.
tvanceplus changed the proposed answer to a comment
ASKER CERTIFIED SOLUTION
Avatar of captainkirk
captainkirk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial