0

I completely don't know how to approach to the issue. While compiling I gets the error: C2280 'Employee &Employee::operator =(const Employee &)': attempting to reference a deleted function.

I've found out that is a common problem but still I can't find any solution. Please take a look at the code:

class Employee
{
public:
    std::string name, profession;
    std::string current_task = "NONE";
    int id, age, warrings;

    std::vector<std::string>& tasks;

    Employee::Employee(std::vector<std::string>& tasks) : tasks(tasks)
    {
        warrings = 0;
    };

    virtual void AssignNewTask(std::string input_string)
    {
        for (unsigned int i = 0; i < tasks.size(); i++)
        {
            if (input_string == tasks[i])
            {
                current_task = input_string;
                std::cout << ">> The new task has been assigned" << std::endl;
                return;
            }
        }

        std::cout << input_string << " does not below to duties list for " << profession << std::endl;
    }
};

class HR : public Employee
{
private:
    static std::vector<std::string> tasks;

public:
    HR::HR() : Employee(tasks)
    {
        Employee::profession = "HR Specialist";
    }
};

class Helpdesk : public Employee
{
private:
    static std::vector<std::string> tasks;

public:
    Helpdesk::Helpdesk() : Employee(tasks)
    {
        Employee::profession = "Helpdesk Technician";
    }
};

std::vector<std::string> HR::tasks = { "HR task" };
std::vector<std::string> Helpdesk::tasks = { "Helpdesk task" };

I'd like to ask for some explanation, details about issue. Maybe does someone know any good tutorial about it? I guess it is about operators overloading, default constructor or copy constructor or something like this. I had been reading about it but it isn't easy subject

mathsicist
  • 181
  • 4
  • 15
  • I feel like some code is missing. Is there any code where you try to set an Employee, HR, or Helpdesk object via =? So any code that looks like this? HR hr; hr = otherHr; – QiMata Apr 01 '16 at 10:49
  • It is your `std::vector& tasks;` it is a reference, and a reference cannot be re-assigned, hence the assignment operator would be ill-formed. – Niall Apr 01 '16 at 10:56
  • @Toothkiller you're right, I pasted just a part of code. But there is not any used `=` operator between objects. I avoid it because I had been guessing that it could be a problem – mathsicist Apr 01 '16 at 11:03
  • @Niall where do I try to re-assigned it? – mathsicist Apr 01 '16 at 11:04
  • @mathsicist in your declaration of class Employee std::vector& tasks; – QiMata Apr 01 '16 at 11:07
  • The reference would have to be re-assigned for the `operator=` to be well formed. The code causing this is in the code you posted, it is somewhere else. Your code, as posted, compiles in the online VC compiler at http://webcompiler.cloudapp.net, any attempt made (possibly even indirectly) to assign one to the other results in the error. An [mcve] would help. – Niall Apr 01 '16 at 11:08
  • what do u mean by assign _even indirectly_ a one object to the other one? Sorry for duplicated post, I'll be more careful next time. – mathsicist Apr 01 '16 at 11:21
  • "even indirectly"; not written by you. Another function, algorithm or container. A statement `e1 = e2` where both are `Employee` is generated somewhere. – Niall Apr 01 '16 at 11:35
  • Duplicates can be hard to find, the [mcve] is more important. – Niall Apr 01 '16 at 11:38
  • Ok, I'll be remember about it. I'm going to find the cause by my own. If I failed I'll post a new question. Thank you – mathsicist Apr 01 '16 at 11:47
  • @Niall I'm not sure if it is not against the rules but I've found the issue. It occurs `employees.erase(employees.begin() + i)` (`employees.pop_back()` and `v.push_back(HR())` works fine). Should I start a new question or just edit this one? – mathsicist Apr 01 '16 at 14:08
  • Start a new question, it sounds like it will be different to this one. – Niall Apr 01 '16 at 14:46

0 Answers0