how to put template class into container

Discussion in 'C++' started by =?utf-8?B?5rW36aOO?=, Aug 17, 2006.

  1. i have made a template class like this,
    template< typename T>
    class CStatus
    {
    public:
    ~CStatus(){}
    void OnRender(CDisplay * pDisplay)
    {
    if ( C24Operator::eek:perator_empty != _status)
    {
    _t->Render(pDisplay);

    }
    }
    C24Operator::status_operator _status;
    T _t;
    public:
    CStatus(C24Operator::status_operator st, T t)
    {
    _t =t;
    _status = st;
    }
    private:
    CStatus( const CStatus&);
    CStatus operator=(const CStatus&);
    };

    Now i want to put its entities into a container ,for example a list
    list<CStatus<>> alist is error, how should i do? thanks ahead
     
    =?utf-8?B?5rW36aOO?=, Aug 17, 2006
    #1
    1. Advertising

  2. =?utf-8?B?5rW36aOO?=

    Rickfjord Guest

    Re: how to put template class into container

    Hi,

    I have had a similar problem where it was not possible to declare a
    std::vector using a std::pair as element type, i.e.

    std::vector<std::pair<...>>

    I solved this by making a typedef of the pair,

    typedef std::pair<...> MyPair;

    and then using this in the vector,

    std::vector<MyPair>

    this solved the problem for me. I hope you get some results from it.

    Best regards,
    Stefan Rickfjord
    M.Sc. Software Engineer


    海风 wrote:
    > i have made a template class like this,
    > template< typename T>
    > class CStatus
    > {
    > public:
    > ~CStatus(){}
    > void OnRender(CDisplay * pDisplay)
    > {
    > if ( C24Operator::eek:perator_empty != _status)
    > {
    > _t->Render(pDisplay);
    >
    > }
    > }
    > C24Operator::status_operator _status;
    > T _t;
    > public:
    > CStatus(C24Operator::status_operator st, T t)
    > {
    > _t =t;
    > _status = st;
    > }
    > private:
    > CStatus( const CStatus&);
    > CStatus operator=(const CStatus&);
    > };
    >
    > Now i want to put its entities into a container ,for example a list
    > list<CStatus<>> alist is error, how should i do? thanks ahead
     
    Rickfjord, Aug 17, 2006
    #2
    1. Advertising

  3. =?utf-8?B?5rW36aOO?=

    Ian Collins Guest

    海风 wrote:
    > i have made a template class like this,
    > template< typename T>
    > class CStatus
    > {
    > public:
    > ~CStatus(){}
    > void OnRender(CDisplay * pDisplay)
    > {
    > if ( C24Operator::eek:perator_empty != _status)
    > {
    > _t->Render(pDisplay);
    >
    > }
    > }
    > C24Operator::status_operator _status;
    > T _t;
    > public:
    > CStatus(C24Operator::status_operator st, T t)
    > {
    > _t =t;
    > _status = st;
    > }
    > private:
    > CStatus( const CStatus&);
    > CStatus operator=(const CStatus&);
    > };
    >
    > Now i want to put its entities into a container ,for example a list
    > list<CStatus<>> alist is error, how should i do? thanks ahead
    >

    Leave a space.

    std::list<CStatus<> > alist;

    --
    Ian Collins.
     
    Ian Collins, Aug 17, 2006
    #3
  4. 海风 schrieb:
    > i have made a template class like this,
    > template< typename T>
    > class CStatus
    > {
    > public:
    > ~CStatus(){}
    > void OnRender(CDisplay * pDisplay)
    > {
    > if ( C24Operator::eek:perator_empty != _status)
    > {
    > _t->Render(pDisplay);
    >
    > }
    > }
    > C24Operator::status_operator _status;
    > T _t;
    > public:
    > CStatus(C24Operator::status_operator st, T t)
    > {
    > _t =t;
    > _status = st;
    > }
    > private:
    > CStatus( const CStatus&);
    > CStatus operator=(const CStatus&);
    > };
    >
    > Now i want to put its entities into a container ,for example a list
    > list<CStatus<>> alist is error, how should i do? thanks ahead
    >


    You can't declare a list of 'any CStatus'. You need a concrete type
    e.g. CStatus<int>:

    std::list<CStatus<int> > alist;

    /S
    --
    Stefan Naewe
    stefan_DOT_naewe_AT_atlas_DOT_de
     
    =?UTF-8?B?U3RlZmFuIE7DpHdl?=, Aug 17, 2006
    #4
  5. =?utf-8?B?5rW36aOO?=

    Kai-Uwe Bux Guest

    ?? wrote:

    > i have made a template class like this,
    > template< typename T>
    > class CStatus
    > {
    > public:
    > ~CStatus(){}
    > void OnRender(CDisplay * pDisplay)
    > {
    > if ( C24Operator::eek:perator_empty != _status)
    > {
    > _t->Render(pDisplay);
    >
    > }
    > }
    > C24Operator::status_operator _status;
    > T _t;
    > public:
    > CStatus(C24Operator::status_operator st, T t)
    > {
    > _t =t;
    > _status = st;
    > }
    > private:
    > CStatus( const CStatus&);
    > CStatus operator=(const CStatus&);
    > };
    >
    > Now i want to put its entities into a container ,for example a list
    > list<CStatus<>> alist is error, how should i do? thanks ahead


    a) You cannot use std::list on a template. You can us standard containers
    only on complete types, e.g., you can use std::list< CStatus<char> >. Also
    note that I did not type "std::list<CStatus<char>>". The compiler would
    interpret the ">>" the wrong way.

    b) More importantly though, you cannot use std::list on any of the various
    CStatus<T> because CStatus<T> does not satisfy the copy-constructible and
    assignable rquirements: your assignment operator and your copy constructor
    are private. That means, you cannot use any of these types with the
    standard containers.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 17, 2006
    #5
  6. Re: how to put template class into container

    Rickfjord wrote:

    > Hi,
    >
    > I have had a similar problem where it was not possible to declare a
    > std::vector using a std::pair as element type, i.e.
    >
    > std::vector<std::pair<...>>
    >
    > I solved this by making a typedef of the pair,
    >
    > typedef std::pair<...> MyPair;
    >
    > and then using this in the vector,
    >
    > std::vector<MyPair>
    >
    > this solved the problem for me. I hope you get some results from it.


    You apparently are unaware of one of the ideosyncrasies of C++ template
    syntax and have therefore fallen into a classic beginner trap. Please
    see the following reference, which explains the problem and proposes a
    possible (future) solution:

    http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1757.html

    Best regards,

    Tom
     
    Thomas Tutone, Aug 17, 2006
    #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. Aguilar, James
    Replies:
    6
    Views:
    420
    Aguilar, James
    Aug 11, 2004
  2. christopher diggins
    Replies:
    16
    Views:
    757
    Pete Becker
    May 4, 2005
  3. Replies:
    1
    Views:
    332
    John Harrison
    Feb 21, 2007
  4. er
    Replies:
    1
    Views:
    395
    Barry
    Sep 5, 2007
  5. A L
    Replies:
    1
    Views:
    512
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page