I need access variables in class from sqlite callback function. It cannot be static because i need acces this variables from other functions. This is my current code.
class fromdb {
private:
string paramdb;
char* errmsg;
string param;
string title;
string creator;
char* bin;
public:
static int callback(void *data, int argc, char **argv, char **azColName){
int lenght;
lenght = sizeof(*argv[3]);
title = *argv[1];
creator = *argv[2];
bin = new char[lenght];
bin = argv[3];
return 0;
}
void getdata() {
string tQuery = "SELECT * FROM test WHERE " + paramdb + "=\"" + param + "\" )";
sqlite3_exec(db, tQuery.c_str(), fromdb::callback, (void*)data, &errmsg);
}
};
logs
undefined reference to `fromdb::title[abi:cxx11]'
undefined reference to `fromdb::creator[abi:cxx11]'
undefined reference to `fromdb::bin'
You're getting undefined references because you are attempting to use non-static members from a static function.
You can still use a
static
function, but you need to pass a member in, as @Richard Critten points out in the comments, or you can use afriend
function.Here I've created a more simple version of your code to demonstrate, using a
static
function like you have, but passing in the member variable:Or as a
friend
function instead: