类中的重载函数(Students())是输出输入的信息,但它不起作用

The overloaded function (Students()) within the class is to output the information inputted, But it does not function

本文关键字:信息 不起作用 输入 输出 函数 Students 重载      更新时间:2023-10-16

我在一个名为 Students 的类中重载了一个函数。输入信息的那个有效,但输出信息的那个不起作用。我确实相信问题主要出在某个地方。代码更糟,我已经解决了很多问题并来到了这个。如果我尝试输出学生的姓名,它只会结束程序。

这些是成员函数和变量:

class ShowStudents
{
public:
//Declaring a struct
struct Employee
{
string name_public = "";
int age_public = 0;
double ID_No_publice = 0;
};
//Array declaration of type Employee
Employee employees[arraysize];

void Students(int size_public)
{
cout << "Input the number of information to be entered: ";
cin >> size_public;
// Reseting the screen
system("cls");
for (int i = 0; i < size_public; i++)
{
cout << "Enter a name: ";
cin >> employees[i].name_public;
cout << endl;
cout << "Enter age: ";
cin >> employees[i].age_public;
cout << endl;
cout << "Enter the salary: ";
cin >> employees[i].salary_public;
cout << endl;
system("cls");
}
}
void Students(Employee employees[arraysize], int& size_public)
{
for (int i = 0; i < size_public; i++)
{
if (i == 0)
{
cout << setw(4) << "Namet" << "Aget" << "Wage" << endl << endl;
}
cout << setw(4) << employees[i].name_public << 't'
<< employees[i].age_public << 't'
<< employees[i].salary_public << endl;
}
}
};
> This is my main function:
int main()
{
//Declared the variables
student.Students(size);
student.Students(student.employees, size);<------ does not output
system("cls");
}

我已经删除了主要部分的一些不那么重要的部分。我想提供一个最小的可重现代码。到目前为止,我已经使用堆栈溢出一个月了,所以如果我的问题不好或不理解,你可以告诉我。我也有一个月零一周的编程时间,所以我可能会问很多可能很愚蠢的问题。请不要生气。

我注意到的第一件事如下(根据评论还有其他一些错误):

在主要声明中,您声明:

int size = 0;

然后致电

student.Students(size);

然而在方法

void Students(int size_public)

按值及其修改传递size_public

cin >> size_public;

在外面没有效果。

然后你打电话:

student.Students(student.employees, size);<------ does not output

但如前所述size值仍然是=0的,因为它的值没有被Students(int size_public)方法修改。

一种可能的解决方法是通过引用传递大小。修改原型:

void Students(int size_public)

void Students(int& size_public)