defining a vector of template class

Hi everyone,

I'm trying to define a vector that contains a template class. I create the class, then I create two objects from it (to validate) and then I want to create a vector that contains, for example the two objects created, but I don´t know how to pass the type of the vector. I tried differents way but always fail. The template has 4 parameters, even it wold be the same with only one.Here`s the code

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
 template <typename f,typename reg,typename id ,typename mod>
 class datos{                 
 public:
   f dato;
   reg tipo;
   id id_reg;
   mod modif;
   datos(f vdato,reg tipo_dato,id tid_reg,mod tid_modif)  
     {dato=vdato;
     tipo=tipo_dato ;
     id_reg=tid_reg;
     modif=tid_modif;}
   };
    
   

int main()  {
int entero=43;
int identificador=99;
float d=98.7645;
int* a;
a=&entero;
int* i;
i=&identificador;
datos <int,char,int,char> mi (*a,'c',*i,'n');
datos <float,char,int,char> mi2 (d,'c',*a,'n');
cout<<mi.dato<<endl;
cout<<mi.tipo<<endl;
cout<<mi.id_reg<<endl;
cout<<mi.modif<<endl;
cout<<mi2.dato<<endl;
cout<<mi2.tipo<<endl;
cout<<mi2.id_reg<<endl;
cout<<mi2.modif<<endl;
       
       
vector <datos<float,char,int,char> > mivector (2);
return 0 ;
}


Thank you in advance.
I suspect its because you don't have a default constructor
You can't.

Types in a container have to be the same. datos<float,char,int,char> and datos<int,char,int,char> are two completely different types.

It'd be like trying to put a float and an int in the same vector -- it just can't be done.
ok, it´s not well said. Sorry english is not my language, I want to do a vector of type <float,char,int,char> and another one for rxample of type <int,char,int,char>

Warnis, About the default constructor ¿did you mean to make a constructor that assigns constant values for example to the parameters of the class? After that How would I call the type inside the vector declaration?

Thank you all
Last edited on
You can make your constructor work as the default constructor by providing default values to the arguments. You can also use an initializer list:
1
2
3
4
5
6
7
8
9
10
11
datos(
  f   vdato     = f(),
  reg tipo_dato = reg(),
  id  tid_reg   = id(),
  mod tid_modif = mod()
  ):
  dato( vdato ),
  tipo( tipo_dato ),
  id_reg( tid_reg ),
  modif( tid_modif )
  { }

Hope this helps.
it helped Duoas, the code complie perfectly and it works. Thank you so much. In addition , I learnt about a (more efficient as I could read, and new for me ) method to create default constructors.

Regards.
Topic archived. No new replies allowed.