Vector of a template class into another class

Hi everybody.
I have a template class which works fine:
1
2
3
4
5
6
7
8
9
template<class T>
class Flag {
	string flagName;
	vector<T> paramList;
	typename vector<T>::iterator it;

	public:
	Flag <T>(string, string, bool);
};

The problem is that I have another class which should store a vector of object of the first class, but I have no idea on how to implement it. It should be something like:
1
2
3
4
5
6
7
8
9
10
11
class CmdLine {

	Module *module;
	vector<Flag<T> > cmdLineVect; // WRONG!!!

	public:
	CmdLine (Module*);
	~CmdLine (void);
	
	void addFlag(Flag<T> );
};

But of course it does not work...
Could you give me some advices? What are the possible ways to achieve my goal?
Thanks in advance!
Last edited on
class CmdLine is not a template class, so the T on these two line here:
vector<Flag<T> > cmdLineVect;
void addFlag(Flag<T> );

have no relevance.

Turn them concrete types - For example
1
2
vector<Flag<int> > cmdLineVect;
void addFlag(Flag<int> );


or turn CmdLine into a template class as well.
1
2
3
4
5
6
7
8
9
10
11
12
 template<typename T>
class CmdLine {

	Module *module;
	vector<Flag<T> > cmdLineVect; 

	public:
	CmdLine (Module*);
	~CmdLine (void);
	
	void addFlag(Flag<T> );
};



Last edited on
Turning them into a concrete type makes no sense to me, because I loose the power of template implementation.

Your second suggestion is not duable too, because if I do this

CmdLine <int> *commandLine = new CmdLine<int> (prog);

I create an object CmdLine with a vector of object Flag<int>. But this is not what I want.

I would like to define a vector that could store for example one Flag<int>, one Flag<float> and one Flag<string>.

Is that possible? I'm wondering if it's possible to do something like that with inheritance...

Or maybe my approach is simply wrong...
Or maybe my approach is simply wrong...
You seem to need a variant. That's been discussed recently.
jsmith's example may help. http://www.cplusplus.com/forum/general/37874/#msg203487
Last edited on
Thank you.

But is there a solution that not involves boost libraries?
You can make up your own variant. If you know the types are (you already said int, float, string) you can just support those.
http://www.cplusplus.com/forum/general/36365/#msg199469
caneta wrote:
I'm wondering if it's possible to do something like that with inheritance...

It is. The idea is to have an abstract Flag class and derive a templated class from it.

1
2
3
4
class BaseFlag {/*...*/};

template <class T>
class Flag : public BaseFlag {/*...*/};

Then, you can do something like this:

1
2
3
4
5
6
vector<BaseFlag*> cmdLineVect;

cmdLineVect.push_back(new Flag<int>(-12));
cmdLineVect.push_back(new Flag<double>(5.5));
cmdLineVect.push_back(new Flag<char>('d'));
//etc... 

caneta wrote:
But is there a solution that not involves boost libraries?

Yes. Check this out -> http://www.cplusplus.com/forum/general/33247/

EDIT: Fixed a horrible syntax error...
Last edited on
I'm getting creazy, trying to create a minimal example with boost.
Here's what I have implemented:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <boost/variant.hpp>

using namespace std;
using namespace boost;

template <class T>
class B
{
	T value;

	public:
	B(T v) { value = v; };
	T getValue() { return value; }
};

typedef variant<B<int>, B<float>, B<double>, B<string> > var_t;

class print_visitor : boost::static_visitor<>
{
	public:
		template<typename T>
			void operator()(T & x) const
			{
				cout << "Print something" << endl;
			}
};

class A
{
	vector<var_t> vec;

	public:
	void addObj(var_t obj) { vec.push_back(obj); }
	var_t getObj(int i) { return vec.at(i); }
};

int main(int argc, char* argv[])
{
	A *container = new A();

	B<int>    elem1(10);

	container->addObj(elem1);

	apply_visitor( print_visitor(), container->getObj(0) );

	return 0;
}

...and here the compilation error:
heterogeneousContainerBoost.cpp: In function ‘int main(int, char**)’:
heterogeneousContainerBoost.cpp:52: error: no matching function for call to ‘apply_visitor(print_visitor, var_t)’
/usr/include/boost/variant/detail/apply_visitor_unary.hpp:54: note: candidates are: typename Visitor::result_type boost::apply_visitor(Visitor&, Visitable&) [with Visitor = print_visitor, Visitable = var_t]
/usr/include/boost/variant/detail/apply_visitor_unary.hpp:70: note:                 typename Visitor::result_type boost::apply_visitor(const Visitor&, Visitable&) [with Visitor = print_visitor, Visitable = var_t]


I don't understand where I am wrong...can you help me?
Topic archived. No new replies allowed.