Class template syntax

  • Thread starter Matthias =?ISO-8859-1?Q?K=E4ppler?=
  • Start date
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

Hi,

in Nicolai Josuttis book about the C++ standard library, class templates are
defined like this:

template<class T>
class MyClass<T>
{
T value;
};

That doesn't work for me (g++ 3.3.4):
templates.cpp:4: error: `MyClass' is not a template
templates.cpp:6: confused by earlier errors, bailing out

I have always used this syntax:

template<class T> // or preferrably 'typename T'
class MyClass // no <T> here
{
T value;
};

Which has always worked for me. I am just curious why the syntax presented
in one of the (if not *the*) most popular books about the C++ standard
library doesn't compile with g++ (which is probably as popular as the book
*g*).

Thanks,
Matthias
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

Matthias said:
Hi,

in Nicolai Josuttis book about the C++ standard library, class templates
are defined like this:

template<class T>
class MyClass<T>
{
T value;
};

That doesn't work for me (g++ 3.3.4):
templates.cpp:4: error: `MyClass' is not a template
templates.cpp:6: confused by earlier errors, bailing out

I have always used this syntax:

template<class T> // or preferrably 'typename T'
class MyClass // no <T> here
{
T value;
};

Which has always worked for me. I am just curious why the syntax presented
in one of the (if not *the*) most popular books about the C++ standard
library doesn't compile with g++ (which is probably as popular as the book
*g*).

Thanks,
Matthias

I just looked at another example, where he didn't notate the <T>. Maybe that
was just a typo...? Whatever.
 
S

Sumit Rajan

Matthias said:
Hi,

in Nicolai Josuttis book about the C++ standard library, class templates are
defined like this:

template<class T>
class MyClass<T>
{
T value;
};

Where exactly in the book did you come across this one?
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

Sumit said:
Where exactly in the book did you come across this one?

Don't have it at hand right now, but it was in the very first chapter I
think. But nevermind, it probably was just a typo, since the other examples
were fine.
 
S

Sumit Rajan

Matthias said:
Don't have it at hand right now, but it was in the very first chapter I
think. But nevermind, it probably was just a typo, since the other examples
were fine.


You are right. There is a typo in Section 2.2.1 (Page 12 and 13). This
is what the author's website has to say about it:



Page 12 and 13, Section 2.2.1
On both pages replace:
template <class T>
class MyClass<T> {
by:
template <class T>
class MyClass {


Regards,
Sumit.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top