Im a begginer on c++, im try to creat an object but im getting an error, and i dont understand what wrong. this is my hpp file + cpp file when im getting the error:
Manager::Manager(const Manager &manager ) :
Worker(manager.name,manager.id,manager.salary){
this->workers=manager.workers;
}
class Manager:public Worker {
private:
std::vector<Worker> workers;
public:
Manager(const char* name, int id, int salary);
Manager(const Manager &manager );
};
error:
In file included from Manager.hpp:5:0,
from Manager.cpp:1:
Worker.hpp:7:7: note: ‘Worker& Worker::operator=(const Worker&)’ is implicitly deleted because the default definition would be ill-formed:
class Worker{
but when im doing this it workes:
Manager::Manager(const Manager &manager ) :
Worker(manager.name,manager.id,manager.salary), workers(manager.workers){
}
someone can tell me why?
edit:
this is the worker class code:
this is worker.hpp
class Worker{
protected:
const std::string name;
const int id;
int salary;
public:
Worker(const std::string& name, int id, int salary);
Worker(const Worker& worker );
};
this is worker.cpp:
#include "Worker.hpp"
Worker::Worker(const std::string &names, int ids, int salarys) :
name(names), id(ids), salary(salarys)
{
}
Worker::Worker(const Worker &worker):
name(worker.name), id(worker.id), salary(worker.salary)
{
}
Worker::~Worker() {
}
std::string Worker::toString(){
std::string s="Worker id:" + id ;
return s;
}
Default assignment operator generated by compiler by performing assignment for each field. So if one of them is const-qualified or is not copyable compiler fails to emit assignment operator. Most likely Worker
class (that you should've posted in your question) contains one or more of such fields.
Note that calling this->workers=manager.workers;
will invoke copy assignment operators for each Worker
stored in worker
, while calling workers(manager.workers)
will invoke copy constructors, which is well-defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With