【问题标题】:Creating multiple objects in a class在一个类中创建多个对象
【发布时间】:2017-08-22 03:48:35
【问题描述】:

我对如何将大量对象放入一个类感到困惑。 所以我们需要读入一个包含时间戳、员工 ID、位置编号、事件代码的文件。输入的一个例子是:

10039865 WHITE99 1 OP
10039876 WHITE99 1 EN
10047500 PINK01 1 EN
10047624 SMITH01 3 EX
10047701 TAN07 2 EN
10048567 DIITZ01 2 OP
10048577 DIITZ01 2 OP
10048587 DIITZ01 2 OP

如何将这些信息设置到类中的对象中? 这是我到目前为止得到的,并从这里卡住了。我们需要使用指向对象的指针数组来编写程序。

class Employee {

    long timestamp;
    string staffID;
    int locNum;
    string eventCode;

public:
    void setValues (long, string, int, string);

};

void Employee::setValues(long timestamp, string staffID, int locNum, string eventCode) {
    this->timestamp = timestamp;
    this->staffID = staffID;
    this->locNum = locNum;
    this->eventCode = eventCode;
}

【问题讨论】:

  • 创建一个对象,并用一行中的正确值调用setValues成员?
  • 离题:“我们需要使用指向对象的指针数组来编写程序” 按要求完成分配,但要知道:在实际编程中您几乎不会想这样做。还解释了为什么this poor sucker 看起来他们正试图通过编码进入一个深洞。
  • 主题:Zip on down to the section on Bitshift Operators (used for Stream I/O) 并阅读。构建到>> 运算符Option 2 from this answer 中,您应该可以使用类似于while(file >> emplyeelist[count]) count++; 的代码
  • “我们需要使用指向对象的指针数组来编写程序。” 那么,创建一个指向 Employee 对象的指针的 array,从文件逐行创建和填充Employee 对象,然后将其地址插入之前创建的array
  • 你能告诉我怎么做吗?我在编码方面还是新手,所以我不确定如何实现。

标签: c++


【解决方案1】:

我将省略一些事情,因为这看起来像是家庭作业,但这会让你走得更远。

需要注意的几点:

  1. 您没有使用构造函数。当然,默认构造函数很好,但它可以帮助您自己制作,尤其是在刚开始时。
  2. 您可能应该使用vector 而不是array

例如:

// Note that I'm making the members public - this is only for demonstration so I don't have to write getters and setters.
class Employee {
    public:
    Employee(long, std::string, int, std::string);
    long timestamp;
    std::string staffID;
    int locNum;
    std::string eventCode;
};

// Here is the constructor.
Employee::Employee(long l, std::string s, int n, std::string s2): timestamp(l), staffID(s), locNum(n),  eventCode(s2){}

至于数组 - 坚持使用 Employee 指针向量可能更明智。如:

typedef Employee * EmployeePointer;    
EmployeePointer employeePtr;
std::vector<EmployeePointer> employeeVec;

然后.push_back() newEmployees 使用你花哨的带有指针的新构造函数。

employeePtr = new Employee(181213, "Bob", 22, "OP");
employeeVec.push_back(employeePtr);

只需将employeePtr 重新用于新员工即可。

employeePtr = new Employee(666732, "Sue", 21, "MA");
employeeVec.push_back(employeePtr);

您会看到创建了一个指向员工对象的指针向量(大多数其他语言无论如何都将其称为数组):

for(auto it = employeeVec.begin(); it != employeeVec.end(); it++){
    std::cout << (*it)->timestamp << " " << (*it)->staffID << " " << (*it)->locNum << " " << (*it)->eventCode << std::endl;
}

其中显示:

181213 Bob 22 OP
666732 Sue 21 MA

如果您出于某种原因不能使用vector,那么使用数组实现它并没有什么不同,除了。

EmployeePointer empPtrArr;

// You'll need to know the size of your array of pointers or else dynamically allocate it, which I don't think you want to do.
empPtrArr = * new EmployeePointer[2];

而且您必须使用基本的 for 循环,而不是我使用的那种花哨的 for(auto ... )


决赛:

  • 对于每一个“新”,都有一个“删除”,否则就会发生内存泄漏
  • 至少有一个#include我留给你看,应该不难找到

【讨论】:

  • @Macmade 同意!虽然 OP 需要一组指向对象的指针,但我不知道 OP 的经验或限制 - 所以认为如果排除向量,这是一种安抚起始水平的简单方法。你的选择是什么?
  • 谢谢!这对我帮助很大。我们的教授限制我们使用数组,因为我们还没有在课堂上讨论过向量或映射。我对指向员工对象的指针有疑问。既然我用的是数组,那我该怎么做呢?
  • @futilecheese28 stackoverflow.com/questions/620843/… 我相信第二个答案会让你得到你想要的。您会注意到这个问题与您自己的问题相似;)
  • @Macmade 确实让事情变得更简单 - 我会尽快更新它
猜你喜欢
  • 2022-11-15
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
相关资源
最近更新 更多