2016-11-30 93 views
0

所以我做了一个结构来保存有关客户端的信息。标题中出现上述编译器错误。如果我陈述任何不正确的情况,我仍然在学习指针,所以请耐心等待。从我所知道的,我的typedef有一个指向客户端实例的指针。这意味着我需要使用 - >而不是。为客户的领域。但是那也行不通,我会得到dereferencing to incomplete type的错误。任何指导或帮助都会很棒!我无法弄清楚的常见C问题:请求会员的东西不是结构或工会

Client.h

#ifndef CLIENT_H 
#define CLIENT_H 

typedef struct client_tag *Client; 

#endif 

Client.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)); 
+0

这是一个指针,使用' - >'not'.'。 –

+0

try&client1-> name [0] ...记住strcpy中client1之前的“&” – morcillo

+0

为什么'client.c'中的typedef struct client_tag * Client;'而不是Client.h'?该typedef除了'Client.c'外本身并不适用于任何地方。 – user2357112

回答

1

我相信问题是如果您的头文件中没有client_tag声明,则不能使用client_tagclient_tag *上的字段。

我的建议是,为client_tagclient.c基于以上的说明,下面的代码编译并运行

client.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 

主要移动结构声明client.h

.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); 

    } 

} 

clients.txt

1 
John Doe 
[email protected] 
1-555-456-7890 
1

惯用答案取决于你是否希望消费者把你的自定义类型为不透明或不。如果它不需要不透明,请使用Mobius的建议,并将完整定义移至标题。

否则,您必须提供返回的成员函数,例如:

char * client_tag_get_name(client_tag *t) { 
    return t->name; 
} 

,并把该声明在你的头。

+0

所以你说的是,如果我保留我的结构定义在c文件中,这意味着它将成为一个私人结构? – Jasmine

+2

@Jasmine:这意味着他们无法计算结构的大小或访问其成员,因为他们无法访问定义。他们可以将指针传递给它,但不能分配它们自己的指针。基本上他们用它做的一切都依赖于你提供的功能,它们可以为它们工作(通过定义,但不能声明那些可以访问结构定义的函数)。 – ShadowRanger

相关问题