我是新来的东京小屋吃了,我编译了示例程序,我收到错误,任何人都可以告诉我为什么会收到此错误invalid conversion from ‘const void*’ to ‘const char*’
#include <tcutil.h>
#include <tctdb.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
int main(int argc, char **argv){
TCTDB *tdb;
int ecode, pksiz, i, rsiz;
char pkbuf[256];
const char *rbuf, *name;
TCMAP *cols;
TDBQRY *qry;
TCLIST *res;
/* create the object */
tdb = tctdbnew();
/* open the database */
if(!tctdbopen(tdb, "casket.tct", TDBOWRITER | TDBOCREAT)){
ecode = tctdbecode(tdb);
fprintf(stderr, "open error: %s\n", tctdberrmsg(ecode));
}
/* store a record */
pksiz = sprintf(pkbuf, "%ld", (long)tctdbgenuid(tdb));
cols = tcmapnew3("name", "mikio", "age", "30", "lang", "ja,en,c", NULL);
if(!tctdbput(tdb, pkbuf, pksiz, cols)){
ecode = tctdbecode(tdb);
fprintf(stderr, "put error: %s\n", tctdberrmsg(ecode));
}
tcmapdel(cols);
/* store a record in a naive way */
pksiz = sprintf(pkbuf, "12345");
cols = tcmapnew();
tcmapput2(cols, "name", "falcon");
tcmapput2(cols, "age", "31");
tcmapput2(cols, "lang", "ja");
if(!tctdbput(tdb, pkbuf, pksiz, cols)){
ecode = tctdbecode(tdb);
fprintf(stderr, "put error: %s\n", tctdberrmsg(ecode));
}
tcmapdel(cols);
/* store a record with a TSV string */
if(!tctdbput3(tdb, "abcde", "name\tjoker\tage\t19\tlang\ten,es")){
ecode = tctdbecode(tdb);
fprintf(stderr, "put error: %s\n", tctdberrmsg(ecode));
}
/* search for records */
qry = tctdbqrynew(tdb);
tctdbqryaddcond(qry, "age", TDBQCNUMGE, "20");
tctdbqryaddcond(qry, "lang", TDBQCSTROR, "ja,en");
tctdbqrysetorder(qry, "name", TDBQOSTRASC);
tctdbqrysetlimit(qry, 10, 0);
res = tctdbqrysearch(qry);
for(i = 0; i < tclistnum(res); i++){
rbuf = tclistval(res, i, &rsiz);
cols = tctdbget(tdb, rbuf, rsiz);
if(cols){
printf("%s", rbuf);
tcmapiterinit(cols);
while((name = tcmapiternext2(cols)) != NULL){
printf("\t%s\t%s", name, tcmapget2(cols, name));
}
printf("\n");
tcmapdel(cols);
}
}
tclistdel(res);
tctdbqrydel(qry);
/* close the database */
if(!tctdbclose(tdb)){
ecode = tctdbecode(tdb);
fprintf(stderr, "close error: %s\n", tctdberrmsg(ecode));
}
/* delete the object */
tctdbdel(tdb);
return 0;
}
答案 0 :(得分:2)
示例文件显然是要编译为C,而不是C ++。强制转换rbuf = tclistval(res, i, &rsiz);
(rbuf
的类型为const char*
)在C中有效,但在C ++中,您需要明确。看起来你设置Eclipse来编译源文件为C ++ - 如果转换是你得到的唯一错误,你可以像这样解决它:
rbuf = (const char*)tclistval(res, i, &rsiz); // explicit cast to const char*
或者更改您的设置以编译为C.
如果它们不支持包含在C ++中,则可能需要用extern C { ... }
包围Tokyo Cabinet头文件。
答案 1 :(得分:1)
你试图填写你的函数中的一个列(char *),类型为void *,如果你看到这个错误来临的行,你就会知道你在做什么。
这是通用的c ++错误,与tokyocabinet无关,这就是我所相信的。
- 干杯
答案 2 :(得分:1)
Tokyocabinet看起来像一个c库。在c中,您可以将任何类型的指针(void *)和(void *)转换为任何类型。像这样:
int *array = malloc(10*sizeof(int));
在c ++中,这是禁止的,你必须手动投射:
int *array = (int *)malloc(10*sizeof(int));