473,804 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class template syntax

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
Jul 22 '05 #1
4 1167
Matthias Käppler wrote:
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.
Jul 22 '05 #2
Matthias Käppler wrote:
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?
--
Sumit Rajan <su*********@gm ail.com>
Jul 22 '05 #3
Sumit Rajan wrote:
Matthias Käppler wrote:
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?


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.
Jul 22 '05 #4
Matthias Käppler wrote:
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.
--
Sumit Rajan <su*********@gm ail.com>
Jul 22 '05 #5

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

Similar topics

1
3347
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
7
2384
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++ newsgroups, "Thinking In C++" to no avail. Background ----------
5
1251
by: rich | last post by:
Hi there, I defined a class template (MyClass) and some member variables and functions, as following: template<class T1, class T2> class MyClass { ... struct m_variable
23
3870
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
4
3338
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
4
5085
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() { return 0;
1
1620
by: toton | last post by:
Hi, I am doing some template specialization for a template class, where specialization is done on a member function. I am not able to get exact syntax for it. The code is give as below, enum DirectionType{ dtX,dtY,dtXD,dtYD }; template<typename T> class Point{
5
5487
by: Marcin Gil | last post by:
Hi! I have the code like this (obvious things like ctor/dtor removed) typedef struct _NODE { int val; int index; } Node;
3
3888
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: #include <iostream>
7
5776
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.