Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Template derivation

Posted on 2001-08-10
5
Medium Priority
?
505 Views
Last Modified: 2008-03-03
Hi,

I created a template class :

template<class T>
class CBaseT
{
 T *m_pT;
public :
 CBase();
 ~CBase();
 T *Get();
 void Set(T *pT);
};

And I derived a class from it :

class Derived : public CBaseT<char>
{
 public :
 Derived() {Set("A");} // Correction, instead of: Set('A');
 ~Derived();
};

The problem is that I got a link error which says that
the class CBaseT<char> is not found.

My question is : Why is that happened , and how can I
fix it that CBaseT<char> will be found ?

 Thanks
 Michael
0
Comment
Question by:MichaelMaromm
[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
5 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6371737
Set('A') passes a const char, but your definition of Set() requires a char*.

You could change the definition to

void Set(T pT);

and it should work.
0
 

Expert Comment

by:Walluce
ID: 6371821
Or you could simply pass in "A" instead of 'A'.
"A" will be a char * because it creates a string of two characters (A and \0) whereas 'A' is just one char.
0
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 6371847
>>Why is that happened , and how can I fix it that
>>CBaseT<char> will be found ?

This is most likely happening because you put the implementation for your code in the *.cpp file.

In other words, you have the {void Set(T *pT)} function defined in the *.cpp file.

For template classes you need to define the functions in the header.

Continue.......
0
 
LVL 30

Expert Comment

by:Axter
ID: 6371874
Continuation:

Although the current C++ standards say that you can put the functions in the *.cpp file, most compilers do not support this capability.

If you put the code in the *.cpp file, then only code that includes the *.cpp file can declare new types of your template class.

An alternative to this is to put a special forward declaration for each type that you will use for the template class.

The syntax for this is the following:

template CBaseT<char>;

The above forward declaration would be put into the *.cpp file that has your template function defined.
You would have to do this for each type if you want to use this method.

I do not recommend using the above method unless you really have a good reason for keeping the template function definition inside your *.cpp file.

It is far easier to just keep the template function difinition in the *.h header file, and this is the common method used by most programmers for template classes.
0
 

Author Comment

by:MichaelMaromm
ID: 6373017
Thank you Axter,
You realy helped me.

Michael
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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.
Suggested Courses

688 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