Template Specialization error

yugpatel

Senior member
Feb 28, 2001
506
0
76
What am I doing wrong in the following code?

I get the following error using gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3:


||=== Build: Debug in TemplateSpecialization (compiler: GNU GCC Compiler) ===|
/home/bp/projects/TemplateSpecialization/TemplateSpecialization/main.cpp|13|error: ‘xyz’ is not a template|
/home/bp/projects/TemplateSpecialization/TemplateSpecialization/main.cpp|13|error: explicit specialization of non-template ‘xyz’|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Code:
#include <iostream>

using namespace std;
template <class T>
class genericType
{
    public:
        genericType(T x){
                cout << x << " is not a character!" << endl;}
};

template<>
class xyz <char>{
    public: xyz(char x){
                cout << x << " is a character." << endl;}
};


int main()
{
    genericType<int> intType(6);
    genericType<double> doubleType(65.7);
    //xyz<char>  charType('Q');
    return 0;
}
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
You cannot change the class name when specializing a template.

Code:
template<>
class genericType<char> {
  // ....
};
 

yugpatel

Senior member
Feb 28, 2001
506
0
76
Thanks for prompt reply. I knew I was doing something wrong and needed extra pair of eyes.