gpt4 book ai didi

常见的 C 问题我无法弄清楚 : request for member in something not a structure or union

转载 作者:太空宇宙 更新时间:2023-11-04 08:08:51 25 4
gpt4 key购买 nike

所以我制作了一个结构来保存有关客户的信息。我得到如上标题所述的编译器错误。我仍在学习指导,所以如果我说错了什么,请多多包涵。据我所知,我的 typedef 有一个指向 Client 实例的指针。这意味着我需要使用 -> 而不是 .对于客户的领域。但这也不管用,我会得到 dereferencing to incomplete type 的错误。任何指导或帮助都会很棒!

客户端.h

#ifndef CLIENT_H
#define CLIENT_H

typedef struct client_tag *Client;

#endif

客户端.c

#include "client.h"


struct client_tag {
char id[5];
char name[30];
char email[30];
char phoneNum[15];
};

以下文件从文件中读取客户端信息并将它们分配给变量:

#include "client.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
Client client1 = malloc(sizeof(Client));
FILE *fp;
ListType clientList;

fp = fopen("clients.txt", "r");

int lineSize = 200;
char* line = malloc(lineSize);
char* line2 = malloc(lineSize);
char* line3 = malloc(lineSize);
char* line4 = malloc(lineSize);


while (fgets(line, lineSize, fp) != NULL) {
strcpy(client1.id, line);
strcpy(client1.name, fgets(line2, lineSize, fp));
strcpy(client1.email, fgets(line3, lineSize, fp));
strcpy(client1.phoneNum, fgets(line4, lineSize, fp));

push(clientList, (void*)&client1);

printf("ID: %s", line);
printf("Name: %s", line2);
printf("Email: %s", line3);
printf("Phone Number: %s\n", line4);

free(line);
free(line2);
free(line3);
free(line4);

line = malloc(lineSize);
line2 = malloc(lineSize);
line3 = malloc(lineSize);
line4 = malloc(lineSize);

}

}

这是我得到的错误:

    clientDriver.c: In function ‘main’:
clientDriver.c:23:17: error: request for member ‘id’ in something not a structure or union
strcpy(client1.id, line);
^
clientDriver.c:24:17: error: request for member ‘name’ in something not a structure or union
strcpy(client1.name, fgets(line2, lineSize, fp));
^
clientDriver.c:25:17: error: request for member ‘email’ in something not a structure or union
strcpy(client1.email, fgets(line3, lineSize, fp));
^
clientDriver.c:26:17: error: request for member ‘phoneNum’ in something not a structure or union
strcpy(client1.phoneNum, fgets(line4, lineSize, fp));

最佳答案

我认为问题在于,如果您的头文件中没有 client_tag 的声明,则您不能在 client_tag *client_tag 上使用字段。

我的建议是将 client_tag 的结构声明从 client.c 移动到 client.h

根据上述说明,编译运行如下代码

客户端.h

#ifndef CLIENT_H
#define CLIENT_H

struct client_tag {
char id[5];
char name[30];
char email[30];
char phoneNum[15];
};

typedef struct client_tag *Client;

#endif

主.c

#include "client.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Minimal list implementation
* Based on functions in your program
*/
typedef struct {
unsigned length;
size_t allocated_size;
void ** items;
} ListType;

/* Push implemented based on the usage in your code */
int push(ListType l, void* item){
// check if we need to allocate memory
if (l.length >= l.allocated_size){
size_t next_size = (l.allocated_size > 0) ? l.allocated_size * 2 : sizeof(void*);
l.items = (void **) realloc(l.items, next_size);
if (l.items == NULL){
return -1; // error
}

l.allocated_size = next_size;
}

// push the item;
l.items[l.length++] = item;
return l.length;
}

int main() {
Client client1 = malloc(sizeof(Client));
FILE *fp;
ListType clientList;

fp = fopen("clients.txt", "r");

int lineSize = 200;
char* line = malloc(lineSize);
char* line2 = malloc(lineSize);
char* line3 = malloc(lineSize);
char* line4 = malloc(lineSize);


while (fgets(line, lineSize, fp) != NULL) {
strcpy(client1->id, line);
strcpy(client1->name, fgets(line2, lineSize, fp));
strcpy(client1->email, fgets(line3, lineSize, fp));
strcpy(client1->phoneNum, fgets(line4, lineSize, fp));

push(clientList, (void*)&client1);

printf("ID: %s", line);
printf("Name: %s", line2);
printf("Email: %s", line3);
printf("Phone Number: %s\n", line4);

free(line);
free(line2);
free(line3);
free(line4);

line = malloc(lineSize);
line2 = malloc(lineSize);
line3 = malloc(lineSize);
line4 = malloc(lineSize);

}

}

客户端.txt

1
John Doe
john.doe@example.com
1-555-456-7890

关于常见的 C 问题我无法弄清楚 : request for member in something not a structure or union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40900410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com