Solved

Templates

Posted on 2000-04-07
14
271 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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