473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Compile Error: Template Specialization Example

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<intmyint (7);
container<charmychar ('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

Dec 20 '06 #1
2 3896
Nick wrote:
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.
char container<char>::uppercase()
{
if ((element>='a')&&(element<='z'))
element+='A'-'a';
return element;
}

int main () {
container<intmyint (7);
container<charmychar ('j');
cout << myint.increase() << endl;
cout << mychar.uppercase() << endl;
return 0;
}
--
Ian Collins.
Dec 20 '06 #2
Nick wrote:
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)
Dec 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Simon | last post by:
Hi, I'm having a problem with templates and specialisation. I'm using it to overload the same function so it can return different things. I can't see what I'm doing wrong, although my compiler...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
1
by: Naveen | last post by:
Hello All. I am trying to compile this code on Borland 5.6.4. The compiler is throwing up this error message: "main.cpp": E2299 Cannot generate template specialization from 'B<T>' at line 12 ...
6
by: bluekite2000 | last post by:
I have Vector<complex<float> > V(5); V.rand(); Vector<float> V1(V); //specialized function here to return norm(V). This works fine Vector<double> V2(5); V2.rand(); Vector<float>...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
14
by: Dave Rahardja | last post by:
Is there a way to generate a series of statements based on the data members of a structure at compile time? I have a function that reverses the endianness of any data structure: /// Reverse...
7
by: yurec | last post by:
Hi I can't compile //#include <boost/function.hpp> class boost::function; UIStl * LoadStlFromAnotherProject(const CString & i_project_name, boost::function<bool (MSAXReader &,UIObject...
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.