[英]ERROR:request for member 'id' in something not a structure or union
I'm writing a function which compares the id of two vertices. 我正在写一个比较两个顶点ID的函数。
/* Returns whether aim and vertex have the same id */
bool id_eq(void *aim, void *vertex) {
if(*(Vertex)aim.id ==*(Vertex)vertex.id){
return true;
}
return false;
}
aim and vertex are two pointers of struct vertex_t. aim和vertex是struct vertex_t的两个指针。
typedef struct graph_t* Graph;
typedef struct vertex_t* Vertex;
/* A vertex is an id, a label and a list of incoming and outgoing edges */
struct vertex_t {
int id;
char *label;
/* A list of vertices representing incoming edges */
List in;
/* A List of vertices representing outgoing edges */
List out;
};
But when I compiled it, an error occurred as 'ERROR:request for member 'id' in something not a structure or union'. 但是,当我编译它时,发生了一个错误,例如“ ERROR:request for member'id'in a not structure or union''。 Could someone please tell me where I went wrong??? 有人可以告诉我我哪里出问题了吗???
The problem you're having is the casts 您遇到的问题是演员
if( (Vertex)aim.id == (Vertex)vertex.id) if( (顶点)aim.id == (顶点)vertex.id)
The function receives two void pointers 该函数接收两个空指针
bool id_eq(void *aim, void *vertex);
Remember when working with structures the -> or . 请记住,使用->或结构时。 will be taken into account before the cast so this should be solved as following 将在投放之前考虑在内,因此应按以下方式解决
if( ((Vertex)aim)->id ==((Vertex)vertex)->id)
I'd recommend the return as following, but that's personal taste: 我建议按以下方式退货,但这是个人喜好:
bool id_eq(void *aim, void *vertex) {
return ((Vertex)aim)->id == ((Vertex)vertex)->id ? true:false;
}
Edit: 编辑:
The "? true : false" is not necessary as the == will already result in a bool but it makes the code a little clearer. 不需要使用“?true:false”,因为==已经会产生布尔值,但是会使代码更清晰。
Change 更改
if(*(Vertex)aim.id ==*(Vertex)vertex.id){
to 至
if((Vertex)aim->id == (Vertex)vertex->id){
or 要么
if((*(Vertex)aim).id == (*(Vertex)vertex).id){
(Vertex)
casts aim
to "a pointer to a struct vertex_t
". (Vertex)
投射aim
到“一个指向结构vertex_t
”。 Then you can get the id
of the struct vertex_t
it points to by using the ->
operator. 然后,您可以使用->
运算符获取struct vertex_t
它的struct vertex_t
的id
。
Also note that *
(deference) has a lower procedure than .
另请注意, *
(遵循)的步骤比的要低 .
and ->
. 和->
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.