我正在学习C并且目前正在学习如何使用一个统一的c文件之外的多个文件来实现抽象数据类型。我有一个项目,在单独的头文件中定义客户端和股票,其中行为在c文件中定义。最后,我将阅读多个文件并使用客户端和股票信息填充多个链接列表。但是,我的main函数中存在可见性问题,我尝试在Client结构上调用sizeof()。
我所做的研究表明,不包括相应的头文件会出现类似的错误。我很肯定我包含了正确的头文件,我也尝试在头文件中定义结构而不是c文件。
老实说,我不确定结构的前向声明意味着什么,因为我认为当它包含在我的主要文件之前的头文件时,它将“拥有关于它的所有信息”。我可以采取哪些步骤来解决此问题?
这是我编译器在编译main时出现的错误:
main.c:32:31: error: invalid application of 'sizeof' to an incomplete type
'Client' (aka 'struct client')
ListType clientList = create(sizeof(Client), compareClients);
^ ~~~~~~~~
./client.h:6:16: note: forward declaration of 'struct client'
typedef struct client Client;
^
1 error generated
我的主要档案:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "client.h"
#include "stock.h"
#include "listADTgen.h"
/* Main function of the program. */
int main(int argc, char const *argv[]) {
if (argc > 5) { // Input validation on number of arguments.
printf("Invalid format. Exiting...\n");
return -1;
}
FILE *clientFile = fopen(argv[1], "r");
if (clientFile == NULL) { // Validate client file.
printf("Error opening client file. Exiting...\n");
return -1;
}
// Create a list of all clients, read from the client file.
int (*clientCompare) (Client *x, Client *y);
clientCompare = compareClients;
ListType clientList = create(sizeof(Client), compareClients);
FILE *stockFile = fopen(argv[2], "r");
if (stockFile == NULL) { // Validate stock file.
printf("Error opening stock file. Exiting...\n");
return -1;
}
// Create a list of all stocks, read from the stock file.
FILE *stockClientFile = fopen(argv[3], "r");
if (stockClientFile == NULL) { // Validate stock_client file.
printf("Error opening stock + client file. Exiting...\n");
return -1;
}
// Utilize previously built client and stock lists to put together the information
// given to use by the stock_client file.
printf("All good!\n");
return 0;
}
client.h:
typedef struct client Client;
Client* createClient(int id, char *name, int phone, char *email);
void deleteClient(Client *c);
int getId(Client *c);
void setId(Client *c, int x);
char* getName(Client *c);
void setName(Client *c, char *pointer);
int getPhone(Client *c);
void setPhone(Client *c, int x);
char* getEmail(Client *c);
void setEmail(Client *c, char *pointer);
int compareClients(Client *x, Client *y);
client.c:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "client.h"
struct client {
int id;
char name[30];
int phone;
char email[30];
};
/* Creates and returns a pointer to a Client object. */
Client* createClient(int id, char *name, int phone, char *email) {
Client *newClient = malloc(sizeof(Client));
setId(newClient, id);
setName(newClient, name);
setPhone(newClient, phone);
setEmail(newClient, email);
return newClient;
}
/* Deletes a given Client by freeing the memory address allocated to them. */
void deleteClient(Client *c) {
free(c);
}
/* Returns the id number of the given Client. */
int getId(Client *c) {
return c->id;
}
/* Sets the id to the given Client. */
void setId(Client *c, int x) {
c->id = x;
}
/* Returns the name of a given Client. */
char* getName(Client *c) {
char *p = c->name;
return p;
}
/* Sets the name of the given Client. */
void setName(Client *c, char *pointer) {
strcpy(c->name, pointer);
}
/* Returns the phone number of the given Client. */
int getPhone(Client *c) {
return c->phone;
}
/* Sets the phone of the given Client. */
void setPhone(Client *c, int x) {
c->phone = x;
}
/* Returns the email of a given Client. */
char* getEmail(Client *c) {
char *p = c->email;
return p;
}
/* Sets the email of the given Client. */
void setEmail(Client *c, char *pointer) {
strcpy(c->email, pointer);
}
/* Compares two clients based on their id number. */
int compareClients(Client *x, Client *y) {
int result = 0;
if (x->id > y->id) {
result = 1;
} else if (x->id < y->id) {
result = -1;
}
return result;
}
答案 0 :(得分:4)
要使sizeof
结构,编译器需要已经&#34;看到&#34;该结构的完整定义。由于结构仅在client.c
中定义,因此编译器在编译main.c
时无法知道结构的大小。
但是,在这种情况下,我认为您的问题是您使用错误的类型分配列表(clientList
)。该列表需要包含指向Client
个对象(Client *
)的指针,而不是实际的Client
个对象;其元素的大小为sizeof(Client *)
,而不是sizeof(Client)
。