尝试传递结构类型的向量但没有控制台输出

Trying to pass a vector of a struct type but no console output

我正在读取文件并将数据存储在结构类型的向量中。我有 3 个不同的功能:

  1. readFile(insert arg here) // 读取文本文件并获取名称和整个星期的工作时间。
  2. bubbleSort(`more arg') // 不言自明
  3. output(`arg') // 输出所述向量的内容

函数原型:

void readFile(vector<Employee *> workers, int numOfEmployees);
void bubbleSort(vector<Employee *> workers, int numOfEmployees);
void output(vector<Employee *> workers, int numOfEmployees);

结构:

struct Employee
{
    string name;
    vector<int> hours;
    int totalHours;
}

主要:

vector<Employee *> workers;
int numOfEmployees = 0;

readFile(workers, numOfEmployees);
bubbleSort(workers, numOfEmployees);
output(workers, numOfEmployees);
cout << endl;

system("pause");
return 0;

读取文件:

ifstream fin;
fin.open("empdata4.txt");
if (fin.fail())
{
    cout << "File failed to open.  Program will now exit.\n";
    exit(1);
}

fin >> numOfEmployees;
workers.resize(numOfEmployees);

for (int row = 0; row < numOfEmployees; row++)
{
    workers[row] = new Employee;
    workers[row]->hours.resize(7);

    fin >> workers[row]->name;

    for (int i = 0; i < 7; i++)
    {
        fin >> workers[row]->hours[i];
    }
}

// 由于显而易见的原因排除冒泡排序

输出:

 for (int i = 0; i < numOfEmployees; i++)
 {
     cout << workers[i]->name << " ";
     for (int x = 0; x < 7; x++)
     {
         cout << workers[i]->hours[x] << " ";
     }
     cout << endl;
 }

控制台输出为空白,减去 main 中的 cout << endl;system("pause"); 我想我大部分都正确设置了所有内容,但我仍然不知道。感谢您的帮助!

编辑:添加函数原型和结构

将函数 headers 更改为

void readFile(vector<Employee *>& workers, int& numOfEmployees);
void bubbleSort(vector<Employee *>& workers, int& numOfEmployees);
void output(vector<Employee *>& workers, int& numOfEmployees);

如果没有引用 &,您将按值传递,因此您对函数内的 vector 和 int 所做的任何修改都不会影响 main 中的 vector 和 int,因此 main 中的 vector 始终为空。

更好的是,甚至不需要 numOfEmployees。

void readFile(vector<Employee *>& workers);
void bubbleSort(vector<Employee *>& workers);
void output(vector<Employee *>& workers);

如需员工人数,请致电workers.size()