【发布时间】:2013-04-18 19:18:34
【问题描述】:
亲爱的 stackoverflow 社区,
这是我第一个使用带有结构的指针的程序,尽管进行了大量研究,但我还是找不到我要找的东西。如果已经回复,请见谅。
我有一个学校项目,我必须定义结构而不是使用指针数组来存储数据。在这个循环中,我收到以下错误:
表达式必须具有指向对象的类型
for (int i = 0; i < nbClerk; i++)
{
cout<<"Number of hours: ";
cin>>c_info->hoursWorked[i];
}
break;
这是整个代码。非常感谢您的帮助
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//structure defining Employee
struct Employee
{
int hoursWorked;
int hourRate;
double overtime;
string name;
int empID;
};
//Function collecting and calculating info
void employeeInfo(int nbOperator, int nbClerk){
char classOfEmployee;
Employee *c_info;
c_info = new (nothrow) Employee[nbClerk];
Employee *o_info;
o_info = new (nothrow) Employee[nbOperator];
cout<<"Select the class of employee (C=Clerk, O=Operator)";
cin>>classOfEmployee;
switch (tolower(classOfEmployee))
{
case 'c':
for (int i = 0; i < nbClerk; i++)
{
cout<<"Number of hours: ";
cin>>c_info->hoursWorked[i];
}
break;
}
}
int main(){
int nb1,nb2;
cout<<"Nb Employee and nb of nb of operator: ";
cin>>nb1>>nb2;
nbEmployee(nb1, nb2);
system("pause");
}
【问题讨论】:
-
请注意,您的程序存在内存泄漏:您使用
new运算符分配了两个数组,但您没有使用delete运算符再次释放内存。不再需要时插入delete[] c_info; delete[] o_info;。