Specialized template outside-class-definition problem during compile time.

I've done an example of 'template specialization'. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// template specialization
#include <iostream>

using namespace std;

// class template:
template <class T>
	class mycontainer {
		private:
			T element;
		public:
			// only prototype
			mycontainer (T);
			T increase ();
	};

// class method definition
template <class T>
	mycontainer<T> :: mycontainer(T arg) {
		element = arg;
	}

template <class T>
	T mycontainer<T> :: increase () {
		return ++element;
	}
	
// class template specialization: 
template <> 
	class mycontainer <char> { // specialized for char type
		private:
			char element;
		public:
			mycontainer(char arg) {
				element = arg;
			}
			char uppercase() {
				if ((element >= 'a') && (element <= 'z')) {
					element += 'A'-'a';
					return element;
				}
				return '0';
			}
	};

// 
int main () {
	mycontainer<int> myint (7);
	mycontainer<char> mychar ('j');
	//
	cout << myint.increase() << endl;
	cout << mychar.uppercase() << endl;
	//
	return 0;
}


The compiler compiled this code successfully, but when I try to move the definition of the constructor and the method outside the class, the compiler cannot compile it.

Here is the code with outside-class-definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// template specialization
#include <iostream>

using namespace std;

// class template:
template <class T>
	class mycontainer {
		private:
			T element;
		public:
			// only prototype
			mycontainer (T);
			T increase ();
	};

// class method definition
template <class T>
	mycontainer<T> :: mycontainer(T arg) {
		element = arg;
	}

template <class T>
	T mycontainer<T> :: increase () {
		return ++element;
	}
	
// class template specialization: 
template <> 
	class mycontainer <char> { // specialized for char type
		private:
			char element;
		public:
			// only prototype
			mycontainer(char);
			char uppercase();
	};

// definition
template <>
	mycontainer<char> :: mycontainer(char arg) {
		element = arg;
	}

template <>
	char mycontainer<char> :: uppercase () {
		if ((element >= 'a') && (element <= 'z')) {
			element += 'A'-'a';
			return element;
		}
		return '0';
	}

// 
int main () {
	mycontainer<int> myint (7);
	mycontainer<char> mychar ('j');
	//
	cout << myint.increase() << endl;
	cout << mychar.uppercase() << endl;
	//
	return 0;
}


The compiler generated these errors:
1
2
... error C2910: 'mycontainer<char>::{ctor}' : cannot be explicitly specialized
... error C2910: 'mycontainer<char>::uppercase' : cannot be explicitly specialized


Even when I add the 'char' between the angle, the compiler still gave errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
template <char>
	mycontainer<char> :: mycontainer(char arg) {
		element = arg;
	}

template <char>
	char mycontainer<char> :: uppercase () {
		if ((element >= 'a') && (element <= 'z')) {
			element += 'A'-'a';
			return element;
		}
		return '0';
	}

With this code, The compiler generated these errors:
1
2
3
4
5
6
7
8
9
10
11
12
... error C2244: 'mycontainer<char>::{ctor}' : unable to match function definition to an existing declaration
        definition
        'mycontainer<char>::mycontainer(char)'
        existing declarations
        'mycontainer<char>::mycontainer(const mycontainer<char> &)'
        'mycontainer<char>::mycontainer(char)'
... error C2244: 'mycontainer<char>::uppercase' : unable to match function definition to an existing declaration
        TemplateSpecialization3.cpp(36) : see declaration of mycontainer<char>::uppercase'
        definition
        'char mycontainer<char>::uppercase(void)'
        existing declarations
        'char mycontainer<char>::uppercase(void)' 


I use the compiler "Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86", and compile the source-code with "cl /clr *.cpp" command.

Did anyone encounter this problem? Why? Is my second code incorrect or the compiler doesn't allow?

Thank you very much :)

Just leave out the template<> part in the definitions.
Last edited on
Thank R0mai, it works perfectly!
Topic archived. No new replies allowed.