I've done an example of 'template specialization'. Here is the code:
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.Code:// 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; }
Here is the code with outside-class-definition:
The compiler generated these errors:Code:// 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; }
Even when I add the 'char' between the angle, the compiler still gave errors.Code:... error C2910: 'mycontainer<char>::{ctor}' : cannot be explicitly specialized ... error C2910: 'mycontainer<char>::uppercase' : cannot be explicitly specialized
With this code, The compiler generated these errors:Code: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'; }
Code:... 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![]()



LinkBack URL
About LinkBacks




