2

In the process of creating a C++ wrapper for anSqlite3 database. Below are two functions that (working together) fill an std::vector with the results from a query; both of which were successful before I tried to template them. I am getting a compilation error (C2664 in VS) for char*[] to the char** which is the argument in the Sqlite3declaration.

I don't have great knowledge of C but from what I collect an array passed as a function argument is rewritten as a pointer, making char*[] and char** equivalent after the conversion, albeit the result is an rvalue. However, that would require that the vector to be a const reference, defeating the goal of populating it with results.

How can this be changed to correctly compile with a successful query? Any other advice or correction would be greatly appreciated. Thanks in advance.

template<class T, class A>
int exec_callback(void *ptr, int argc, char *argv[], char *names[]) {
    vector<T, A> *vec = reinterpret_cast<vector<T, A> *>(ptr);
    vec->push_back(T(int(argv[0]), string(argv[1]), atoi(argv[2]), atoi(argv[3]),
    atoi(argv[4]), atoi(argv[5])));
    return 0;
}

template<class T, class A>
void selectAllList(vector<T, A>& vec, sqlite3 *db, const char* statement) {
    char *errmsg = NULL;
    sqlite3_exec(db, statement, exec_callback, &vec, &errmsg);
    if (errmsg) {
        printf("error: %s!\n", errmsg);
        return;
    }
    else {
        fprintf(stdout, "Operation done successfully\n");
    }
    list(vec);
}

Edit:

Changing char *argv[], char *names[] to char **argv, char **names has no effect. Throwing the same error: "cannot convert argument 3 from int (__cdecl *)(void *,int,char **,char **) to int (__cdecl *)(void *,int,char **,char **)".

Austin
  • 195
  • 1
  • 4
  • 13
  • What does the type of `argv` have to do with the vector? – CL. Dec 15 '15 at 12:39
  • @CL. It is the third argument from the `sqlite3.c` implementation for the `sqlite3_exec` function. Written as `int (*callback)(void*,int,char**,char**), /* Callback function */` (line 602). I believe it maps to the row number vertically, and its index moves to the next value horizontally. – Austin Dec 15 '15 at 13:03
  • This has nothing to do with *your* vector. Anyway, that error message does not make sense (the types are literally the same). Works for me with a different compiler. Try `exec_callback`. – CL. Dec 15 '15 at 14:09
  • @CL. My thoughts exactly. No luck with `exec_callback`. You got it verbatim as above? Can I ask which compiler? – Austin Dec 15 '15 at 15:58
  • Borland C++ 5.1.1. Not that this helps you. – CL. Dec 15 '15 at 16:09

0 Answers0