template class in STL container

Discussion in 'C++' started by Johan, Sep 22, 2004.

  1. Johan

    Johan Guest

    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
     
    Johan, Sep 22, 2004
    #1
    1. Advertising

  2. "Johan" <> wrote in message
    news:...
    > 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
     
    John Harrison, Sep 22, 2004
    #2
    1. Advertising

  3. Johan

    David Hilsee Guest

    "Johan" <> wrote in message
    news:...
    > 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
     
    David Hilsee, Sep 23, 2004
    #3
  4. Johan

    Sachin Garg Guest

    "John Harrison" <> wrote in message news:<>...
    > "Johan" <> wrote in message
    > news:...
    > > 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]
     
    Sachin Garg, Sep 23, 2004
    #4
  5. "Sachin Garg" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message

    news:<>...
    > > "Johan" <> wrote in message
    > > news:...
    > > > 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
     
    John Harrison, Sep 23, 2004
    #5
  6. Johan

    Greg Comeau Guest

    In article <>,
    John Harrison <> wrote:
    >"Sachin Garg" <> wrote in message
    >news:...
    >> "John Harrison" <> wrote in message

    >news:<>...
    >> > "Johan" <> wrote in message
    >> > news:...
    >> > ....
    >> > 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?
     
    Greg Comeau, Sep 23, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maitre Bart
    Replies:
    2
    Views:
    551
    Maitre Bart
    Feb 11, 2004
  2. christopher diggins
    Replies:
    16
    Views:
    789
    Pete Becker
    May 4, 2005
  3. Replies:
    4
    Views:
    832
    Daniel T.
    Feb 16, 2006
  4. wolverine
    Replies:
    2
    Views:
    485
    Marcus Kwok
    Jul 24, 2006
  5. A L
    Replies:
    1
    Views:
    531
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page