Solved

Template derivation

Posted on 2001-08-10
5
502 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 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

739 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