containers and template classes

A

Alfonso Morra

if I have a template class declared as ff:

template <typename T>
class A {
public:
//usual ctors and dtor cut to save space
T& get( void ) ;
void set( const T&) ;

private:
T x ;
};


How I can I store such an object in a vector like this:

std::vector< A > vector_of_a ; //<- this is pseudocode
 
V

Victor Bazarov

Alfonso said:
if I have a template class declared as ff:

It's not a template class. It's a class template.
template <typename T>
class A {
public:
//usual ctors and dtor cut to save space
T& get( void ) ;
void set( const T&) ;

private:
T x ;
};


How I can I store such an object in a vector like this:

std::vector< A > vector_of_a ; //<- this is pseudocode

You can't. std::vector requires _a_type_ as its first argument, not
a template. Think about it. What "object" are you going to store?
What is 'T'? If it's something concrete, like 'int', then you need
to specify it. If it's not anything concrete, then how would the
vector know how much storage to allocate for it?

V
 
P

Peter Julian

Alfonso Morra said:
if I have a template class declared as ff:

template <typename T>
class A {
public:
//usual ctors and dtor cut to save space
T& get( void ) ;

should be:
T get() const;
void set( const T&) ;

private:
T x ;
};


How I can I store such an object in a vector like this:

std::vector< A > vector_of_a ; //<- this is pseudocode

The std::vector container can't guess what class A is storing. Try:

std:vector< A<int> > vector_of_n;
std:vector< A<double> > vector_of_d;
 

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
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top