新的新类对象的函数;错误:非静态成员引用必须相对于特定对象
The function of a new new class object; error: A nonstatic member reference must be relative to a specific object
我想创建一个雇用(创建一个新的类对象)新员工的函数。我决定使用默认构造函数(如果不是好决定,请纠正我)。我有错误Employee::Hire(employees);
#include <iostream>
#include <string>
#include <vector>
class Employee
{
private:
std::string name;
public:
Employee::Employee()
{
std::cout << "test" << std::endl;
}
void Employ(std::vector<Employee> &v)
{
v.push_back(Employee());
}
};
int main()
{
std::vector<Employee> employees;
Employee::Employ(employees);
system("pause");
}
如何解决?
此函数:
void Employ(std::vector<Employee> &v)
{
v.push_back(Employee());
}
应该移出Employee
的类定义。 成员函数对类的实例进行操作,但您打算在没有任何实例的情况下调用它。
或者,该功能可以static
,但是恕我直言,首选样式是仅使用非成员。
相关文章:
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- 我们可以有一个 setter 成员函数作为从 const 对象引用的 const 吗?
- C++对象引用返回不同的值
- 对象引用中的字段以不同的方法返回不同的值
- 通过向构造函数其他对象引用页面来创建对象
- 为什么在对象引用恒定时允许更改为另一个类的指针的成员变量
- 如何在C++中读取 HDF5 对象引用
- C++ 未定义的对象引用
- 从函数返回 libconfig 类设置对象引用
- qt vsaddin错误对象引用未设置为对象的istanse
- 在这种情况下,为什么使用 *此必需的对象引用返回对象引用
- 基本C - 构造contaning对象引用,并将其作为方法参数传递
- C :将对象引用作为参数给出错误:呼叫class :: function()的匹配函数
- C++OOP基础知识-是否正确返回对象引用
- 使用对象引用(可能)在调用destructor后使用对象引用
- 创建返回对象引用的成员函数的最简单方法
- 如何在C++上使用对象引用删除对象
- JNI/Android NDK-维护全局对象引用
- 为什么有些类方法返回"*this"(self的对象引用)?
- 在C++中返回伪对象引用的规则