親愛的計算器有益社會,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