Solved

Templates

Posted on 2000-04-07
14
265 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

9 Experts available now in Live!

Get 1:1 Help Now