Solved

Template derivation

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

Free Trending Threat Insights Every Day

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.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

21 Experts available now in Live!

Get 1:1 Help Now