Replacing object in vector of derived objects “no matching function to call”

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.

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
void Promote(vector<shared_ptr<Employee>>& sourceEmployee) {

auto it = std::find_if(sourceEmployee.begin(), sourceEmployee.end(),
                              [&sourceEmployee, id](const auto &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:
static const int num = 0;
};

class Worker : public Employee {

protected:
static const int 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)...); }
Last edited on
The error says that you try to initialize a Worker with a shared_ptr object.
You want to initialize the Worker with Employee object, don't you?
Topic archived. No new replies allowed.