C++ 表达式必须具有指向对象的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16091335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Expression must have pointer-to-object type
提问by Larry
Dear stackoverflow helpful community,
亲爱的 stackoverflow 乐于助人的社区,
This is my first program using a pointer with a structure, and despite lots of research, I was not able to find what I was looking for. Please forgive me if this has already been responded to.
这是我第一个使用带有结构的指针的程序,尽管进行了大量研究,但我还是找不到我要找的东西。如果这已经得到回应,请原谅我。
I have a project for school where I have to define structures than use pointers array to store data. In this loop, I get the following error :
我有一个学校项目,我必须定义结构而不是使用指针数组来存储数据。在这个循环中,我收到以下错误:
Expression must have pointer-to-object type
表达式必须具有指向对象的类型
for (int i = 0; i < nbClerk; i++)
{
cout<<"Number of hours: ";
cin>>c_info->hoursWorked[i];
}
break;
here's the whole code. thank you very much for your help
这是整个代码。非常感谢您的帮助
#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");
}
采纳答案by Alexander Shukaev
You probably meant:
你可能的意思是:
c_info[i].hoursWorked;
since c_info
is an array, by doing c_info[i]
you'll access the i
-th instance (object) of Employee
class in c_info
array, and then obtain hoursWorked
through .
operator.
因为c_info
是一个数组,通过这样做,c_info[i]
您将访问数组i
中Employee
类的第 -th 个实例(对象)c_info
,然后hoursWorked
通过.
运算符获取。
Now you can clearly see that your variant simply doesn't make sense, as hoursWorked
is just an integral type and not an array, and therefore you cannot apply []
operator to it.
现在您可以清楚地看到您的变体根本没有意义,因为hoursWorked
它只是一个整数类型而不是数组,因此您不能对其应用[]
运算符。
回答by leemes
c_info
is a pointer to an Employee. You can assign a single allocated object to such a pointer or, in your case, multiple ones (new
with the array syntax). So it points to an array of Employees.
c_info
是一个指向 Employee 的指针。您可以将单个分配的对象分配给这样的指针,或者在您的情况下分配多个对象(new
使用数组语法)。所以它指向一个Employees数组。
You dereferenced that pointer. Since it points to an array of (multiple) Employees, it also points to the first entry. Then you access an integer member variable, which is still possible. But then you try to use the array subscript operator ([]
) on an integer value, which is not possible.
您取消了该指针的引用。由于它指向一个(多个)雇员数组,它也指向第一个条目。然后你访问一个整数成员变量,这仍然是可能的。但是随后您尝试对[]
整数值使用数组下标运算符 ( ),这是不可能的。
You probably meant to access the member variable of the i
-th entry of your allocated array. So you have to turn this around: First use the array subscript operator, then access the member on that particular Employee.
您可能打算访问已i
分配数组的第 -th 个条目的成员变量。所以你必须扭转这个局面:首先使用数组下标运算符,然后访问该特定员工的成员。
c_info[i]
in low-level words means: Take the pointer c_info
, add i
times the size of the type it points to (so it points to the i
-th entry) and dereference that address. This means, that c_info[i]
actually is the Employee at the i
-th index (but not a pointer).
c_info[i]
用低级的话来说是指:取指针c_info
,i
乘以它指向的类型的大小(因此它指向i
第 -th 个条目)并取消引用该地址。这意味着,c_info[i]
实际上是i
第 -th 个索引处的 Employee (但不是指针)。
Then you want to access a member of that employee. If it still was a pointer, you would have to use the arrow operator, but since you used the array subscript operator ([i]
), you already have dereferenced it, you the point operator is the correct one:
然后您想访问该员工的成员。如果它仍然是一个指针,则必须使用箭头运算符,但是由于您使用了数组下标运算符 ( [i]
),因此您已经取消了对它的引用,因此点运算符是正确的:
cin >> c_info[i].hoursWorked;