I have a function read()
that reads a file and store information into a map. However, whenever the function calls map.insert()
, it gives me an error.
Employee
and Volunteer
are two customs classes with only a few variables. For example, If I call
ifstream fin;
std::map<std::string, Employee*> employees;
fin.open("Employee.txt");
read<Employee, EMPLOYEE_SIZE>(fin, employees);
It gives the following error:
std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert(std::initializer_list<std::pair<const std::string,Employee *>>)': cannot convert argument 1 from 'initializer list' to 'std::initializer_list<std::pair<const std::string,Employee *>>'
. What am I doing wrong here?
Here is the function(Both Employee
and Volunteer
has the same base class:)
template <typename T, int T_LINE_SIZE>
inline void read(std::ifstream& in, std::map<std::string, T*>& input) {
std::string name = "";
std::string ID="";
std::string line;
double salary;
while (getline(in,line)) {
if (typeid(T) == typeid(Volunteer)) {
name = line.substr(0, 19);
ID = line.substr(20, T_LINE_SIZE);
input.insert({name,new Volunteer(ID,name)}); //error happens here
}else if (typeid(T) == typeid(Employee)) {
name = line.substr(0, 19);
ID = line.substr(20, 29);
salary = std::stod(line.substr(30, T_LINE_SIZE));
input.insert({ name,new Employee(ID, name, salary) }); //error happens here
}
}
}
Aucun commentaire:
Enregistrer un commentaire