I have class Employee and derived class Worker and Intern. I store them in vector<shared_ptr<Employee>> Firm; Now I want to promote Intern to Worker by replacing derived object Intern in vector with Worker, with all the fields from Employee saved.
void Promote(vector<shared_ptr<Employee>>& sourceEmployee) {
auto it = std::find_if(sourceEmployee.begin(), sourceEmployee.end(),
[&sourceEmployee, id](constauto &obj) { return obj->getID() == id; });
if (it != sourceEmployee.end()) {
auto index = std::distance(sourceEmployee.begin(), it);
switch(sourceEmployee[index]->getnum()) { // returning num / recognizing specified class obj
case 0: { // It's Intern, lets make him Worker
auto tmp0 = std::move(*it);
(*it).reset();
*it = std::make_shared<Worker>(tmp0); // CAUSING ERROR
cout << "Employee " << id << " has been promoted" << endl;
break;
}
class Employee {
//basic c-tors etc.
protected:
int employeeID;
std::string Name;
std::string Surname;
int Salary;
bool Hired;
};
class Intern : public Employee {
protected:
staticconstint num = 0;
};
class Worker : public Employee {
protected:
staticconstint num = 1;
};
So basically I need to destroy Intern object and create Worker instead in same place.
1 2
error: no matching function for call to ‘Worker::Worker(std::shared_ptr<Employee>&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }