【发布时间】:2015-06-08 20:42:42
【问题描述】:
我是编码新手,我正试图了解这个程序有什么问题:
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;
}
在 main 中有这个:
Company* company = new Company();
a = new DeveloperEmployee(description, project);
int id = a->getID();
cout << *a << endl; //Developer ID = 2, project = hw5
company += a;
我有这个错误:
error: invalid operands of types 'Company*' and 'DeveloperEmployee*' to binary 'operator+'|
【问题讨论】:
-
请避免指针和愚蠢的继承(公司->员工)
-
您发布的错误消息和发布的代码不匹配。发布的代码中没有调用
operator+。 -
@RSahu:有。但它并不明确。