Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to put template class into container

Reply
Thread Tools

how to put template class into container

 
 
=?utf-8?B?5rW36aOO?=
Guest
Posts: n/a
 
      08-17-2006
i have made a template class like this,
template< typename T>
class CStatus
{
public:
~CStatus(){}
void OnRender(CDisplay * pDisplay)
{
if ( C24Operator: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

 
Reply With Quote
 
 
 
 
Rickfjord
Guest
Posts: n/a
 
      08-17-2006
Hi,

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

std::vector<std:air<...>>

I solved this by making a typedef of the pair,

typedef std:air<...> 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: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


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-17-2006
海风 wrote:
> i have made a template class like this,
> template< typename T>
> class CStatus
> {
> public:
> ~CStatus(){}
> void OnRender(CDisplay * pDisplay)
> {
> if ( C24Operator: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.
 
Reply With Quote
 
=?UTF-8?B?U3RlZmFuIE7DpHdl?=
Guest
Posts: n/a
 
      08-17-2006
海风 schrieb:
> i have made a template class like this,
> template< typename T>
> class CStatus
> {
> public:
> ~CStatus(){}
> void OnRender(CDisplay * pDisplay)
> {
> if ( C24Operator: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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-17-2006
?? wrote:

> i have made a template class like this,
> template< typename T>
> class CStatus
> {
> public:
> ~CStatus(){}
> void OnRender(CDisplay * pDisplay)
> {
> if ( C24Operator: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
 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      08-17-2006
Rickfjord wrote:

> Hi,
>
> I have had a similar problem where it was not possible to declare a
> std::vector using a std:air as element type, i.e.
>
> std::vector<std:air<...>>
>
> I solved this by making a typedef of the pair,
>
> typedef std:air<...> 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/WG...005/n1757.html

Best regards,

Tom

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
Copying a template class into another template class object with different no of templates erictham115@gmail.com C++ 1 02-21-2007 05:58 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Proper way to put an abstract base class into a container Aguilar, James C++ 6 08-11-2004 04:50 AM



Advertisments