[英]C - error: incomplete definition of type 'struct name_s'
I try to implement a bloom filter in C. I split the file in two header one public and one private. 我尝试在C中实现一个bloom过滤器。我将文件拆分为两个标题,一个是public,一个是private。 The first contain the function's signature e the typedef of the struct and the second the definition of the struct.
第一个包含函数的签名e结构的typedef,第二个包含struct的定义。
This is the code of the below heade: 这是下面的代码:
bloom_filter_private.h bloom_filter_private.h
#ifndef BLOOM_FILTER_PRIVATE_H
#define BLOOM_FILTER_PRIVATE_H
#include <pds/bloom_filter.h>
struct bloom_filter_s {
int *bit_vector;
int dimension;
};
#endif /* BLOOM_FILTER_PRIVATE_H */
bloom_filter.h bloom_filter.h
#ifndef BLOOM_FILTER_H
#define BLOOM_FILTER_H
typedef struct bloom_filter_s* bloom_filter_t;
bloom_filter_t bloom_filter_create(int const dimension);
int bloom_filter_destroy(bloom_filter_t bloom_filter);
#endif /* BLOOM_FILTER_H */
In the end I have a file where I implemented the code of the create/destroy function: 最后我有一个文件,我实现了create / destroy函数的代码:
bloom_filter.c bloom_filter.c
#include <stdio.h>
#include <stdlib.h>
#include "bloom_filter_private.h"
bloom_filter_t bloom_filter_create(int const dimension) {
if ( dimension >= 0) {
int i;
bloom_filter_t bloom_filter = malloc(sizeof(struct bloom_filter_s));
if ( bloom_filter == NULL) {
perror("Unable to create a bloom filter");
abort();
}
bloom_filter->bit_vector = NULL;
if ( dimension > 0 ) {
bloom_filter->bit_vector = (int *) malloc(dimension * sizeof(int));
if ( bloom_filter->bit_vector == NULL ) {
perror("Unable to create a bit vector");
abort();
}
for ( i = 0; i < dimension; i++ )
bloom_filter->bit_vector[i] = 0;
}
bloom_filter->dimension = dimension;
return bloom_filter;
}
else return NULL;
}
int bloom_filter_destroy(bloom_filter_t bloom_filter) {
if ( bloom_filter != NULL) {
free(bloom_filter->bit_vector);
free(bloom_filter);
return 1;
}
return -1;
}
The file bloom_filter.c
and bloom_filter.h
are in the same folder called src
. 文件
bloom_filter.c
和bloom_filter.h
位于名为src
的同一文件夹中。 The file bloom_filter_private.h
is in the subfolder pdf
of the folder include
. 文件
bloom_filter_private.h
位于文件夹include
的子文件夹pdf
中。
The problem is that I try to create a empty bloom filter with the following simple code, I have this error: 问题是我尝试使用以下简单代码创建一个空的bloom过滤器,我有这个错误:
error: incomplete definition of type 'struct bloom_filter_s'
错误:类型'struct bloom_filter_s'的定义不完整
Therefore I can't access to the structur's fileds. 因此我无法访问structur的文件。
main.c main.c中
#include <stdio.h>
#include <pds/bloom_filter.h>
int main(void) {
int i;
bloom_filter_t bloom_filter = bloom_filter_create(10);
for ( i = 0; i < 10; i++ )
printf("[%d] =: %d\n", i, bloom_filter->bit_vector[i]);
return 0;
}
The whole point of putting the definition of the struct into a private file is so users can't see the contents. 将结构的定义放入私有文件的全部意义在于用户无法看到内容。 If you want to give users access to these fields you need to create accessor functions in your library:
如果要授予用户访问这些字段的权限,则需要在库中创建访问者函数:
bloom_filter.c: bloom_filter.c:
int get_vector_element(bloom_filter_t bloom_filter, int index)
{
return bloom_filter->bit_vector[index];
}
bloom_filter.h: bloom_filter.h:
int get_vector_element(bloom_filter_t bloom_filter, int index);
The you can do the following in main
: 您可以在
main
执行以下操作:
for ( i = 0; i < 10; i++ )
printf("[%d] =: %d\n", i, get_vector_element(bloom_filter, i));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.