作为类对象容器的静态向量
我已经看到关于此问题的许多问题和答案,但仍然无法解决我的问题。我应该如何初始化一个静态向量?我也要求你检查我正确使用默认构造。我不是说检查它是否工作,因为我知道它确实有效。我只是想知道这是否是一个优雅的实现?作为类对象容器的静态向量
class Employee
{
private:
static std::vector<Employee> employee;
std::string name;
int age;
Employee::Employee()
{
std::string localName;
int localAge;
std::cout << "So... do you want to hire a new employee? Let's look at CVs " << std::endl;
localName = "Marek"; //GenerateName();
localAge = 21; //these function is not ready yet. it'd be just a rand()
std::cout << "I've got one. What do u think about " << localName << " age " << localAge << "?" << std::endl;
int decision;
do
{
std::cout << "Do you want hire him [1] or not [2] ? " << std::endl;
std::cin >> decision;
switch (decision)
{
case 1:
name = localName;
age = localAge;
decision = 0;
break;
case 2:
employees.erase(employees.end());
decision = 0;
break;
default:
std::cout << "Incorrect option. Try again" << std::endl;
}
} while (decision != 0);
}
public:
static void Employ()
{
employees.push_back(Employee::Employee());
}
};
int main()
{
Employee::Employ();
system("pause");
}
当我运行它时,您的代码不起作用。除了添加包括和固定的一些错字,我不得不加入这一行:
std::vector<Employee> Employee::employee;
不过,我不认为这是最好的解决办法。为了清楚起见,员工不应该包含员工向量,但应该是员工。如果你想要一个雇员的矢量,你可以在主要(或其他地方)申报一个。如果你希望你的员工的载体有一些附加的功能,比如你写的互动员工加入的功能,你可以做这样的:
class EmployeeForce: public std::vector<Employee>
{
void interactivelyAddEmployee();
...
};
...
EmployeeForce myStaff;
感谢您的回复。我已经在另一个论坛上写过,而且我的答案和你一样。看看我的代码现在看起来如何。如果您会为我检查它,我将不胜感激。我现在做得好吗? – mathsicist
我在代码中看不到任何更改。 ? –
现在看,请 – mathsicist
我已经改变了整个概念。问题是我不需要在类中放入对象的容器(在这种情况下:std :: vector)。我现在知道,类只应该包含关于单个对象的信息;它不应该知道关于他人的任何事情。
class Employee
{
public:
std::string name;
int age;
};
std::ostream& operator<<(std::ostream& out, const std::vector<Employee>& v)
{
out << "[";
for (size_t i = 0; i < v.size(); ++i)
{
out << "Name: " << v[i].name << " age: " << v[i].age;
if (i != v.size() - 1)
out << ", ";
}
out << "]";
return out;
}
Employee generate_random_employee(Employee obj)
{
obj.name = "Marek"; //GenerateName();
obj.age = 21; //these function is not ready yet. it'd be just a rand()
return obj;
}
int main()
{
int decision;
std::string name;
std::vector<Employee> employees;
Employee some_new_employee;
std::cout << "Welcome mrs. manager. What do you want to do today, sir?" << std::endl << std::endl;
do
{
std::cout << "Hire sombody [1], Fire somebody [2], Exit [0] " << std::endl;
std::cin >> decision;
switch (decision)
{
case 1:
some_new_employee = generate_random_employee(some_new_employee);
if (should_i_employ(some_new_employee))
{
employees.push_back(some_new_employee);
}
break;
case 2:
std::cout << "Who do you want to fire?" << std::endl;
std::cin >> name;
if (is_the_employee_exist(employees, name))
{
if (are_u_sure())
{
}
}
else
std::cout << "" << std::endl;
break;
case 3:
std::cout << employees << std::endl;
break;
case 0:
std::cout << "Good bye, sir" << std::endl;
break;
default:
std::cout << "There is not these option. Try again " << std::endl;
}
} while (decision != 0);
system("pause");
}
尽管这可能会回答您的问题(您自己的),但它只是代码而没有任何解释。一个好的答案应该说明解决问题的方法。 – crashmstr
好吧......我会记住它的。我会编辑它 – mathsicist
_“我已经看到很多有关这个问题的问题和答案,但我仍然无法解决我的问题”_你能告诉我们,所有这些答案都无济于事吗?如果在你的用例中存在某些特定的东西,那么再次重现常见的答案似乎毫无意义。对于它的价值,我看不到那会是什么。另外,我并没有真正看到_why_您在这里使用“静态”成员。为什么不能在'main'中实例化一个不错的'EmployeesCollection'类? –
我请你原谅?我不明白'EmployeesCollection'类是什么意思。我试过创建'静态无效InstantiateVector();'但我完全不理解它。我也发现了一些** boost **库,但我已经决定使用标准工具解决问题 – mathsicist
我在问你为什么认为你需要一个'static'成员,如果你真的这么做,为什么所有你已经阅读的许多答案并没有帮助你。无需“恳求我的赦免”,谢谢。 –