在C++中执行嵌套STRUCT时出错



我正在解决C++中嵌套结构的问题,但是我在访问structDate的成员函数时出错,即:void showdata((首先,我制作了一个Data结构,然后在其中添加了名为void showdata的成员函数,该函数将显示我的另一个struct Employee的数据。但是,我无法在主程序中访问此成员功能。

错误:错误C2660"Date::showdata":函数不接受0个参数
,因此基本上在代码的最后一行中得到一个错误,即:d.显示数据((

这是我的代码:

struct Date
{
int year, month, date;
void showdata(int empid, string name, double salary, int year, int month,int date)
{
cout << "ttEmployee's Data:n";
cout << "Employee ID --> " << empid << endl;
cout << "Employee Name --> " << name << endl;
cout << "Salary --> " << salary << endl;
cout << "Joining Date -- >" << date << "-" << month << "-" << year << endl;
}
};
struct Employee
{
int empid;
string name;
double salary;
Date joiningdate;
};
int main()
{
//Q3
Employee e;
Date d;
cout << "Enter Employee ID = ";
cin >> e.empid;
cout << "Enter Employee Name = ";
cin >> e.name;
cout << "Enter Salary = ";
cin >> e.salary;
cout << "Enter Employee joining year = ";
cin >> e.joiningdate.year;
cout << "Enter Employee joining month = ";
cin >> e.joiningdate.month;
cout << "Enter Employee joining date = ";
cin >> e.joiningdate.date;
d.showdata();
}

您的意思是将成员函数移到Employee类中吗?

struct Date
{
int year, month, date;
};
struct Employee
{
int empid;
string name;
double salary;
Date joiningdate;
void showdata()
{
cout << "ttEmployee's Data:n";
cout << "Employee ID --> " << empid << endl;
cout << "Employee Name --> " << name << endl;
cout << "Salary --> " << salary << endl;
cout << "Joining Date -- >" << joiningdate.date << "-" << joiningdate.month << "-" << joiningdate.year << endl;
}
};
int main()
{
//Q3
Employee e;
cout << "Enter Employee ID = ";
cin >> e.empid;
cout << "Enter Employee Name = ";
cin >> e.name;
cout << "Enter Salary = ";
cin >> e.salary;
cout << "Enter Employee joining year = ";
cin >> e.joiningdate.year;
cout << "Enter Employee joining month = ";
cin >> e.joiningdate.month;
cout << "Enter Employee joining date = ";
cin >> e.joiningdate.date;
e.showdata();
}

您在Date结构中定义了showdata方法以接受6个参数,但在main中调用该方法时,您提供了0个参数。要正确调用showdata方法,您需要为其提供正确的参数集:

d.showdata(e.empid, e.name, e.salary, e.joiningdate.year, e.joiningdate.month, e.joiningdate.date);

还要确保检查传入的参数类型是否与定义要接受的方法的参数类型匹配。

最新更新