I have the following code and I am getting an error that says the parameter map
has an incomplete type. The variable strings
is an smap
declared as an extern in one of the header files included where the following code resides.
我有下面的代码,我得到一个错误,说参数映射有一个不完整的类型。变量字符串是一个smap,在包含以下代码的一个头文件中声明为extern。
How can I fix this, or what does the error mean, or what are common causes for this error?
如何修正这个,误差是什么意思,或者这个误差的常见原因是什么?
void print_strings(smap map);
void emit_strings(AST *ast) {
/* TODO: Implement me. */
printf(".data\n");
print_strings(strings);
}
void print_strings(smap map){
for (size_t i = 0; i < map -> num_buckets; i += 1) {
for (size_t j = 0; j < map -> buckets[i].num_pairs; j+=1) {
printf("str%d: .asciiz %c\n",label_count,map ->buckets[i].pairs[j].key);
label_count ++;
}
}
label_count = 0;
}
1 个解决方案
#1
3
I previously posted an incorrect answer, but user @mafso graciously steered me back in the right direction.
我之前发布了一个错误的答案,但用户@mafso优雅地引导我回到正确的方向。
The problem I overlooked before is that your print_strings
function attempts to directly access member variables of the smap
type. Since this is an incomplete type (i.e. it is defined in a separate source file), you cannot do this. You need to work with whatever functions are declared in the header in order to access the buckets
and their internal values.
我之前忽略的问题是,您的print_strings函数尝试直接访问smap类型的成员变量。因为这是一个不完整的类型(例如,它是在一个单独的源文件中定义的),所以您不能这样做。您需要使用在header中声明的任何函数,以便访问bucket及其内部值。
A typical header might look like this:
典型的标头可能是这样的:
#ifndef SMAP_H
#define SMAP_H
#include "bucket.h"
#include <stdlib>
typedef struct string_map *smap;
smap smap_create(size_t num_buckets);
bucket *smap_get_buckets(smap map):
void *smap_add_bucket(smap map, bucket b);
#endif
In this example, instead of using map->buckets
to access the buckets
member, you would call the getter: smap_get_buckets(map)
.
在本例中,不使用map->bucket来访问bucket成员,而是调用getter: smap_get_bucket (map)。
The truth is, this is all speculation, because you still haven't provided any hints about the files that declare and define smap
.
事实是,这都是猜测,因为您还没有提供任何关于声明和定义smap的文件的提示。
#1
3
I previously posted an incorrect answer, but user @mafso graciously steered me back in the right direction.
我之前发布了一个错误的答案,但用户@mafso优雅地引导我回到正确的方向。
The problem I overlooked before is that your print_strings
function attempts to directly access member variables of the smap
type. Since this is an incomplete type (i.e. it is defined in a separate source file), you cannot do this. You need to work with whatever functions are declared in the header in order to access the buckets
and their internal values.
我之前忽略的问题是,您的print_strings函数尝试直接访问smap类型的成员变量。因为这是一个不完整的类型(例如,它是在一个单独的源文件中定义的),所以您不能这样做。您需要使用在header中声明的任何函数,以便访问bucket及其内部值。
A typical header might look like this:
典型的标头可能是这样的:
#ifndef SMAP_H
#define SMAP_H
#include "bucket.h"
#include <stdlib>
typedef struct string_map *smap;
smap smap_create(size_t num_buckets);
bucket *smap_get_buckets(smap map):
void *smap_add_bucket(smap map, bucket b);
#endif
In this example, instead of using map->buckets
to access the buckets
member, you would call the getter: smap_get_buckets(map)
.
在本例中,不使用map->bucket来访问bucket成员,而是调用getter: smap_get_bucket (map)。
The truth is, this is all speculation, because you still haven't provided any hints about the files that declare and define smap
.
事实是,这都是猜测,因为您还没有提供任何关于声明和定义smap的文件的提示。