views:

87

answers:

3

hello, 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;

}

A: 

Hi,

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

Koteswara sarma
ya thats right but if i compile `gcc -I/usr/local/include tc_example.c -o tc_example \ -L/usr/local/lib -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc it works fine but not in eclipse`
ragiv
A: 

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));
Dadam
A: 

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.

AndiDog