I want to store several structs in a vector, but the struct has a template so I get a compile error, how can I store the structs in the vector?

Example:

template<class First, class Second> struct foo
{
     First f;
     Second s;
};

std::vector<foo> bar; //error!

Recommended Answers

All 3 Replies

You have to fill in the template to instantiate it: std::vector< foo< int, double > > bar;

Yeah, I thought of that but I wanted to be able to use it to store any type of foo .

Is there not a way around this?

Yes certainly you can do that but you have to resolve the instance at some point. So perhaps you want it is another templated class.

template<class First, class Second> 
struct foo
{
     First f;
     Second s;
};

template<typename A,typename B>
struct Other
{
  std::vector< foo< A, B > > bar;
  std::vector< foo< B, B > > barX;
};

Note that you still have to have the compiler figure out the exact versions that you will need. You have only delayed the choise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.