473,324 Members | 2,581 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,324 software developers and data experts.

template class in STL container

Hi,

Hi like to create a linked list with a template class ( see below ). How to
make one using the STL container list.

I want to create a linked list with different kind of types i.e.
DataVariant<int>, DataVariant<string> etc, etc

#include <list>

using namespace std;

list<....what to put here for your template class ....> DataList;

template <class T>class DataVariant
{
public :
DataVariant(T value);

T getValue();

private :
T value;
};

template <class T>DataVariant<T> :: DataVariant(T value)
: value(value)
{
}

template <class T>T DataVariant<T> :: getValue()
{
return value;
}
regards Johan
Jul 22 '05 #1
5 1627

"Johan" <me@knoware.nl> wrote in message
news:10*************@corp.supernews.com...
Hi,

Hi like to create a linked list with a template class ( see below ). How to make one using the STL container list.

I want to create a linked list with different kind of types i.e.
DataVariant<int>, DataVariant<string> etc, etc

#include <list>

using namespace std;

list<....what to put here for your template class ....> DataList;


list< DataVariant<int> > DataList1;
list< DataVariant<string> > DataList2;

The only thing to watch out for is that you must put a space between the
first > and the second >.

john
Jul 22 '05 #2
"Johan" <me@knoware.nl> wrote in message
news:10*************@corp.supernews.com...
Hi,

Hi like to create a linked list with a template class ( see below ). How to make one using the STL container list.

I want to create a linked list with different kind of types i.e.
DataVariant<int>, DataVariant<string> etc, etc

#include <list>

using namespace std;

list<....what to put here for your template class ....> DataList;

template <class T>class DataVariant
{
public :
DataVariant(T value);

T getValue();

private :
T value;
};

template <class T>DataVariant<T> :: DataVariant(T value)
: value(value)
{
}

template <class T>T DataVariant<T> :: getValue()
{
return value;
}


If you want to store objects of different types in the same container, see
the FAQ (http://www.parashift.com/c++-faq-lite) section 34 ("Container
classes and templates"), question 4 "How can I build a <favorite container>
of objects of different types?"). If you choose to go with the "second
case" (no common base) approach in the FAQ, then boost::any
(http://www.boost.org/doc/html/any.html) may be useful.

--
David Hilsee
Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2r*************@uni-berlin.de>...
"Johan" <me@knoware.nl> wrote in message
news:10*************@corp.supernews.com...
Hi,

Hi like to create a linked list with a template class ( see below ). How

to
make one using the STL container list.

I want to create a linked list with different kind of types i.e.
DataVariant<int>, DataVariant<string> etc, etc

#include <list>

using namespace std;

list<....what to put here for your template class ....> DataList;


list< DataVariant<int> > DataList1;
list< DataVariant<string> > DataList2;

The only thing to watch out for is that you must put a space between the
first > and the second >.

john


I thought this 'space' problem was only in my VC compiler. Or is it
true across others like gcc too? Is there a specific reason why this
issue cant be removed from compilers?

Sachin Garg [India]
Jul 22 '05 #4

"Sachin Garg" <sc*****@gmail.com> wrote in message
news:b3*************************@posting.google.co m...
"John Harrison" <jo*************@hotmail.com> wrote in message

news:<2r*************@uni-berlin.de>...
"Johan" <me@knoware.nl> wrote in message
news:10*************@corp.supernews.com...
Hi,

Hi like to create a linked list with a template class ( see below ).
How to
make one using the STL container list.

I want to create a linked list with different kind of types i.e.
DataVariant<int>, DataVariant<string> etc, etc

#include <list>

using namespace std;

list<....what to put here for your template class ....> DataList;


list< DataVariant<int> > DataList1;
list< DataVariant<string> > DataList2;

The only thing to watch out for is that you must put a space between the
first > and the second >.

john


I thought this 'space' problem was only in my VC compiler. Or is it
true across others like gcc too? Is there a specific reason why this
issue cant be removed from compilers?

Sachin Garg [India]


It's true on all compilers. The rules of C++ say that >> is the right shift
operator, so you must put in a space.

These rules may change in the future, and no doubt some compilers are smart
enough to figure out what you really meant, but as it stands at the moment a
space is required.

john
Jul 22 '05 #5
In article <2r*************@uni-berlin.de>,
John Harrison <jo*************@hotmail.com> wrote:
"Sachin Garg" <sc*****@gmail.com> wrote in message
news:b3*************************@posting.google.c om...
"John Harrison" <jo*************@hotmail.com> wrote in message

news:<2r*************@uni-berlin.de>...
> "Johan" <me@knoware.nl> wrote in message
> news:10*************@corp.supernews.com...
> ....
> list< DataVariant<string> > DataList2;
>
> The only thing to watch out for is that you must put a space between the
> first > and the second >.


I thought this 'space' problem was only in my VC compiler. Or is it
true across others like gcc too? Is there a specific reason why this
issue cant be removed from compilers?


It's true on all compilers. The rules of C++ say that >> is the right shift
operator, so you must put in a space.

These rules may change in the future, and no doubt some compilers are smart
enough to figure out what you really meant, but as it stands at the moment a
space is required.


Comeau C++'s current error on something like that is:

error: space required between adjacent ">" delimiters of
nested template argument lists (">>" is the right shift operator)

so it's able to figure out some of the cases, and eventually probably
will accept it (silently or with a warning, depending upon the mode
requested, and whether this is resolved in some manner in C++0x).
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 22 '05 #6

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

Similar topics

7
by: Senthilvel | last post by:
Hi folks, I was trying to learn the standard library algorithms where i got stuck with the templates. I wrote one function to eliminate the duplicates(from The C++ Programming Language 3rd...
11
by: Alexander Stippler | last post by:
Hi, I have a little problem to design some template classes in a realizable way. I have some Container classes and some Proxy classes (proxies for elements). Looks like this: // P is the...
7
by: Siemel Naran | last post by:
Hi. I have a function template <class InputIter, class OutputIter> void f(InputIter begin, InputIter end, OutputIter result); With c of type char* and cc of type const char*, the code...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
2
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
2
by: Nick | last post by:
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...
7
by: aaragon | last post by:
Hi everyone, The idea is quite simple: generate a container with random values in it. For that, I decided to create a class that I called RandomContainer that inherits from a container (with...
3
by: Anonymous | last post by:
Could someone please explain template template classes, showing: 1). Why they are needed / i.e what problem do they solve ? 2). A simple example I have read various articles etc, but it still...
8
by: Konstantin | last post by:
I have two possible implementations of my class: template <typename T> class MyContainer { public: typedef typename std::set<T>::const_iterator const_iterator; void add( T id ) {...
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
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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.