【问题标题】:How to make use of structure in another class?如何在另一个类中使用结构?
【发布时间】:2014-02-06 07:17:54
【问题描述】:

好的,大家好,我目前的任务有问题

我有 2 个类,即员工类和一个 gm 类。

在员工类中,我使用了一个结构,即

struct eInfo
{
    char username [80];
    char password [80];
    int eid;
};    

但是,在我的 gm 类中,我的函数依赖于员工类中的结构。例如,

void addEmployee(fstream&, int, eInfo&);
void delEmployee(fstream&, int, eInfo&);
void editEmployee(fstream&, int, eInfo&);

我想如何利用它?

我尝试的是重新声明一个

struct eInfo
{
    char username [80];
    char password [80];
    int eid;
};

在 gm 类中。但这并不好。没有语法错误,但是当我使用

fstream 文件; afile.open("EmployeeInfo.dat", ios::in | ios::out | ios::binary);

文件结果是自己清空了。

知道如何解决这个问题吗??

【问题讨论】:

  • 为什么不让二等同学成为一等同学的朋友
  • 你为什么不把eInfo结构体从课堂上拿出来?

标签: c++ class struct fstream


【解决方案1】:

如果结构eInfo 声明为inside employe,那么当您在employe 之外时,您应该将其称为employe::eInfo。如果你在gm 中重新声明它,那么编译器会认为employe::eInfogm::eInfo 是两个不同的类。

【讨论】:

    【解决方案2】:

    在适当的头文件中声明 eInfo,然后将该头文件包含在您的 gm 类中。要创建结构,请使用此语法。变量名称只是您在创建结构时碰巧使用的任何内容的填充物。

    eInfo myStruct = {theUsername, thePassword, theEid};
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多