Compile Error: Template Specialization Example

N

Nick

I'm learning C++ and ran into a compile error using Visual C++ 2005
Express on the following example program (located at
http://www.cplusplus.com/doc/tutorial/templates.html):

// template specialization
#include <iostream>
using namespace std;

template <class T>
class container {
T element;
public:
container (T arg) {element=arg;}
T increase () {return ++element;}
};

template <>
class container <char> {
char element;
public:
container (T arg) {element=arg;}
char uppercase ();
};

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

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

The compile errors:

Compiling...
template4.cpp
..\template4.cpp(17) : error C2146: syntax error : missing ')' before
identifier 'arg'
..\template4.cpp(17) : error C2146: syntax error : missing ';' before
identifier 'arg'
..\template4.cpp(17) : error C2460: 'container<char>::T' : uses
'container<char>', which is being defined
.\template4.cpp(19) : see declaration of 'container<char>'
..\template4.cpp(17) : error C2059: syntax error : ')'
..\template4.cpp(17) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
..\template4.cpp(17) : error C2473: 'arg' : looks like a function
definition, but there is no parameter list.
..\template4.cpp(17) : error C2065: 'arg' : undeclared identifier
..\template4.cpp(17) : error C4430: missing type specifier - int
assumed. Note: C++ does not support default-int
..\template4.cpp(17) : error C2864: 'container<char>::element' : only
static const integral data members can be initialized within a class
..\template4.cpp(18) : error C2628: 'container<char>' followed by 'char'
is illegal (did you forget a ';'?)
..\template4.cpp(19) : error C2059: syntax error : '}'
..\template4.cpp(19) : error C2143: syntax error : missing ';' before
'}'
..\template4.cpp(19) : error C2059: syntax error : '}'
..\template4.cpp(22) : error C2039: 'uppercase' : is not a member of
'container<char>'
.\template4.cpp(19) : see declaration of 'container<char>'
..\template4.cpp(27) : error C2912: explicit specialization; 'char
uppercase(void)' is not a specialization of a function template
..\template4.cpp(24) : error C2065: 'element' : undeclared identifier
..\template4.cpp(31) : error C2664: 'container<char>::container(const
container<char> &)' : cannot convert parameter 1 from 'char' to 'const
container<char> &'
Reason: cannot convert from 'char' to 'const container<char>'
No constructor could take the source type, or constructor
overload resolution was ambiguous
..\template4.cpp(33) : error C2039: 'uppercase' : is not a member of
'container<char>'
.\template4.cpp(19) : see declaration of 'container<char>'

Could someone help me to clarify what the problem is?

TIA
 
I

Ian Collins

Nick said:
I'm learning C++ and ran into a compile error using Visual C++ 2005
Express on the following example program (located at
http://www.cplusplus.com/doc/tutorial/templates.html):

// template specialization
#include <iostream>
using namespace std;

template <class T>
class container {
T element;
public:
container (T arg) {element=arg;}
T increase () {return ++element;}
};

template <>
class container <char> {
char element;
public:
container (T arg) {element=arg;}

container (char arg) {element=arg;}
char uppercase ();
};

template <>

Remove the line above, uppercase() isn't a member specialisation.
 
P

Pete Becker

Nick said:
I'm learning C++ and ran into a compile error using Visual C++ 2005
Express on the following example program (located at
http://www.cplusplus.com/doc/tutorial/templates.html):

if ((element>='a')&&(element<='z'))
element+='A'-'a';
return element;

Ian gave what looks like a valid answer. I just want to mention that
this tutorial is somewhat misleading here: the preceding code makes
unwarranted assumptions about character representations. It works fine
for ASCII, but doesn't necessarily work for other representations. Use
the library function toupper instead of writing your own.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top