how to put template class into container

G

Guest

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
 
R

Rickfjord

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
 
I

Ian Collins

海风 said:
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;
 
G

Guest

海风 said:
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
 
K

Kai-Uwe Bux

?? said:
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
 
T

Thomas Tutone

Rickfjord said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top