亲爱的计算器有益社会,C++表达式必须具有指针到对象类型
这是使用与结构的指针我的第一个程序,尽管大量的研究,我是不是能找到什么,我一直在寻找因为,但请原谅我,如果这已经回应。
我有一个学校,我必须定义比使用指针数组结构来存储数据的项目。在这个循环中,我得到错误“表达式必须有指针到对象类型”
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;删除[] o_info;'当你不再需要它时。 – leemes