Solved

Templates

Posted on 2000-04-07
14
274 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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