Reputation: 31
What is the correct way to initialize an instance of an object within matrix of struct?
My constructor Cita(int, int)
need two parameters to create a new instance, but when I compile the program my shell told me I have to compile with -std=c++0x or -std=gnu++0x
but I'm not allowed to do that. Here's the error..
Dia.cpp: In constructor ‘Dia::Dia(int, int, int)’:
Dia.cpp:9:56: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
Dia.cpp:9:75: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
Dia.cpp:9:75: error: deducing from brace-enclosed initializer list requires #include <initializer_list>
And here's my code..
Dia.cpp
horario_funcionarios = vector_horario_funcionarios (numero_funcionarios,
vector_horario(numero_horas, {true, Cita(0,0)}) );
Dia.hpp
struct hora_dia {
bool habil;
Cita cita;
};
typedef vector<hora_dia> vector_horario;
typedef vector<vector_horario> vector_horario_funcionarios;
class Dia {
private:
int id;
vector_horario_funcionarios horario_funcionarios;
.
.
.
Cita.cpp
Cita::Cita(int id, int nivel) {
this->id = id;
this->nivel = nivel;
}
.
.
Upvotes: 1
Views: 181
Reputation: 254471
The correct way to initialise a temporary POD class is to enable C++11 support and include <initializer_list>
, as the compiler says. But you say you're not allowed to use modern C++ for some reason, so you'll have to do it the hard way.
One possiblity is to give hora_dia
a constructor:
struct hora_dia {
hora_dia(bool habil, Cita cita) : habil(habil), cita(cita) {}
//...
};
horario_funcionarios = vector_horario_funcionarios (numero_funcionarios,
vector_horario(numero_horas, hora_dia(true, Cita(0,0))) );
or possibly a default constructor, if that makes sense:
struct hora_dia {
hora_dia() : habil(true), cita(0,0) {}
//...
};
horario_funcionarios = vector_horario_funcionarios (numero_funcionarios,
vector_horario(numero_horas) );
However, this means that the structure is no longer POD, which might be something you want. In that case, you could use a temporary object:
hora_dia hd = {false, Cita(0,0)};
horario_funcionarios = vector_horario_funcionarios (numero_funcionarios,
vector_horario(numero_horas, hd) );
or a factory function:
hora_dia make_hora_dia(bool habil, Cita cita) {
hora_dia hd = {habil, cita};
}
horario_funcionarios = vector_horario_funcionarios (numero_funcionarios,
vector_horario(numero_horas, make_hora_dia(true, Cita(0,0))) );
None of these options are ideal, which is why uniform initialisation was added to the language. You should try to persuade whoever is forbidding you from using the modern language to relax and embrace its benefits.
Upvotes: 0
Reputation: 6451
In C++98, initializer_list is not supported i.e vectors or other containers cannot be initialized using a list {..}
.
horario_funcionarios = vector_horario_funcionarios (numero_funcionarios,
vector_horario(numero_horas, {true, Cita(0,0)}) );
As g++
says, add #include <initializer_list>
and compile with -std=c++0x
.
Upvotes: 1