In article <7b9vm0$7pq$1...@nnrp1.dejanews.com>,
sl...@hotmail.com wrote:
> Is there a way to implement the function print in the following code:
>
> template <class TreeType>
> class test
> {
> public:
> template <class KeyType>
> void print(KeyType myKey);
> // {
> // cout << myKey;
> // }
> };
>
> I tried doing this but I got some errors:
>
> template <class TreeType>
> template <class KeyType>
> void test<TreeType>::print(KeyType myKey)
> {
> cout << myKey;
> }
>
> -----------== Posted via Deja News, The Discussion Network
==----------
> http://www.dejanews.com/ Search, Read, Discuss, or Start Your
Own
>
Hi,
Here is an example.
The TestClass class is template class.
TestClass has member methods :
void show () - not-template method of template class,
void print () - template method of template class.
Alex
########################################################
######## 1. main.C #####################################
########################################################
#include <stl.h>
template <typename TreeType>
class TestClass
{
public :
TestClass ()
{
cout << __PRETTY_FUNCTION__
<< " : Constructor"
<< endl;
// __PRETTY_FUNCTION__ is
// predefined variable in GNU gcc/g++
}
~TestClass ()
{
cout << __PRETTY_FUNCTION__
<< " : Destructor"
<< endl;
}
// not-template method of template class
void show ()
{
cout << __PRETTY_FUNCTION__ << endl;
}
// template method of template class
template <typename KeyType>
void print (KeyType myKey_i)
{
cout << __PRETTY_FUNCTION__ << endl;
}
};
class AAA
{
public :
AAA () {}
~AAA () {}
};
class FFF
{
public :
FFF () {}
~FFF () {}
};
//==================
int main ()
{
AAA aaa;
FFF fff;
//---------------------
TestClass<int> ttt1;
ttt1.show ();
ttt1.template print<char> ('x');
ttt1.print('x');
ttt1.template print<FFF> (fff);
ttt1.print (fff);
//---------------------
TestClass<AAA> ttt2;
ttt2.show ();
ttt2.template print<char> ('x');
ttt2.print('x');
ttt2.template print<FFF> (fff);
ttt2.print (fff);
//==================
return 0;
}
########################################################
######## 2. The results of the running #################
########################################################
TestClass<int>::TestClass<int>() : Constructor
void TestClass<int>::show<int>()
void TestClass<int>::print<char>(char)
void TestClass<int>::print<char>(char)
void TestClass<int>::print<FFF>(class FFF)
void TestClass<int>::print<FFF>(class FFF)
TestClass<AAA>::TestClass<AAA>() : Constructor
void TestClass<AAA>::show<AAA>()
void TestClass<AAA>::print<char>(char)
void TestClass<AAA>::print<char>(char)
void TestClass<AAA>::print<FFF>(class FFF)
void TestClass<AAA>::print<FFF>(class FFF)
TestClass<AAA>::~TestClass<AAA>() : Destructor
TestClass<int>::~TestClass<int>() : Destructor
########################################################
######## 3. Compiler & System ##########################
########################################################
g++ -v : gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
uname -a : SunOS tibamsun8 5.6 Generic_105181-09
sun4m sparc SUNW,SPARCstation-5
########################################################
######## END of EXAMPLE ################################
########################################################
Appendix. Question :
The __PRETTY_FUNCTION__ printed :
[snip]
void TestClass<int>::show<int>()
void TestClass<int>::print<char>(char)
[snip]
Why not
void TestClass<int>::show()
void TestClass<int>::print<char>(char)
or
void TestClass<int>::show<int>()
void TestClass<int>::print<int, char>(char).
Thanks in advance,
Alex