Solved

Template derivation

Posted on 2001-08-10
5
500 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
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 50 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

15 Experts available now in Live!

Get 1:1 Help Now