gpt4 book ai didi

c - 在迭代 bson 时访问 value.type

转载 作者:可可西里 更新时间:2023-11-01 10:03:55 26 4
gpt4 key购买 nike

我正在尝试遵循 libbson API 文档。但我好像弄错了什么。 documentation声明你可以做:

const bson_value_t *value;

value = bson_iter_value (&iter);

if (value.type == BSON_TYPE_INT32) {
printf ("%d\n", value.value.v_int32);
}

但是当我尝试用它编译实际代码时,出现以下错误:

example1.c:34:64: error: request for member ‘type’ in something not a structure or union

这里是实际的代码:

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>


int
main (int argc,
char *argv[])
{
mongoc_client_t *client;
mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
const bson_t *doc;
const bson_value_t *value;
bson_t *query;
char *str;
bson_iter_t iter;
bson_type_t type;
mongoc_init ();

client = mongoc_client_new ("mongodb://localhost:27017/");
collection = mongoc_client_get_collection (client, "test", "test");
query = bson_new ();
cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);

while (mongoc_cursor_next (cursor, &doc)) {
str = bson_as_json (doc, NULL);
if (bson_iter_init (&iter, doc)) {
while (bson_iter_next (&iter)) {
printf ("Found element key: \"%s\"\n", bson_iter_key (&iter));
type = bson_iter_type (&iter);
printf("type %d\n", (int)type);
value = bson_iter_value (&iter);
printf("Found values of type %d", value.type);
}
}
bson_free(str);
}

bson_destroy (query);
mongoc_cursor_destroy (cursor);
mongoc_collection_destroy (collection);
mongoc_client_destroy (client);

return 0;
}

最佳答案

value 是一个指针。您需要取消引用它:

value->type

(*value).type

除此之外,根据the documentation , bson_value_t 没有名为 type 的成员。也许他们的意思是

value->value_type

关于c - 在迭代 bson 时访问 value.type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28557541/

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