来自“const void*”的无效转换到“const char*”在 tokoyo Cabate 表数据库中

发布于 2024-09-27 06:16:20 字数 2388 浏览 0 评论 0原文

我是东京小屋的新手,我编译了示例程序,但遇到了一个错误,谁能告诉我为什么会出现此错误从“const void*”到“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;

}

i am new to tokyo cabin ate and i compile the example program and i am getting an error can any one tell me why i get this error 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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み青杉依旧 2024-10-04 06:16:20

示例文件显然旨在编译为 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。

你可能需要包围带有 extern C { ... } 的 Tokyo Cabinet 头文件(如果它们本身不支持包含在 C++ 中)。

The example file is obviously intended to be compiled as C, not as C++. The cast rbuf = tclistval(res, i, &rsiz); (rbuf is of type const char*) is valid in C, but in C++, you need to be explicit. It seems like you set up Eclipse to compile the source file as C++ - if the cast is the only error you get, you can solve it like so:

rbuf = (const char*)tclistval(res, i, &rsiz); // explicit cast to const char*

Or change your settings to compile as C.

It might be possible that you need to surround the Tokyo Cabinet header files with extern C { ... } if they don't support inclusion in C++ themselves.

忱杏 2024-10-04 06:16:20

您正在尝试填写函数中类型为 void* 的列之一(char*),如果您看到出现此错误的行,您将了解您在做什么。

这是通用的 C++ 错误,与 tokyabinet 无关,这就是我所相信的。

--
干杯

you are trying to fill in one of the columns(char*) in your functions which is of type void* , if you see line at which this error is coming, you will get to know where you are doing.

This is generic c++ error, nothing to do with tokyocabinet, this is what I believe.

--
Cheers

下雨或天晴 2024-10-04 06:16:20

东京内阁看起来像交流图书馆。在 c 中,您可以将任何类型的指针强制转换为 (void *) 和 (void *) 到任何类型。像这样:

int *array = malloc(10*sizeof(int));

在c++中,这是禁止的,你必须手动强制转换:

int *array = (int *)malloc(10*sizeof(int));

Tokyocabinet looks like a c library. In c, you can cast pointer of any type to (void *) and (void *) to any type. Like this:

int *array = malloc(10*sizeof(int));

In c++, this is forbidden, you have to cast manually:

int *array = (int *)malloc(10*sizeof(int));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文