I'm new to coding and I'm trying to understand what is wrong with this program:
class Company:public Employee{
private:
std::vector<Employee*> _table;
public:
Company& operator+=(const Employee* emp) {
_table.push_back(emp->clone());
return *this;
}
virtual Company* clone() const {
return new Company(*this);
}
virtual Company& setDescription(std::string des){
_des=des;
return *this;
}
with this in main:
Company* company = new Company();
a = new DeveloperEmployee(description, project);
int id = a->getID();
cout << *a << endl; //Developer ID = 2, project = hw5
company += a;
and I have this error :
error: invalid operands of types 'Company*' and 'DeveloperEmployee*' to binary 'operator+'|
You are applying the operator '+=' on a pointer to Company instead of a Company instance or reference to instance. Dereference the pointer, for example
(*company) += a;
will compile without complaint.Please do not overload operators on pointers, overload on const references first, and take it from that.