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.
It cannot be static because I need access this variables from other functions
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 a friend
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:
class artwork
{
private:
std::string title;
std::string creator;
public:
static int populateFromDB(void* object, int, char** data, char**)
{
if (artwork* const art= static_cast<artwork*>(object))
{
art->title = data[1];
art->creator = data[2];
}
return 0;
}
};
artwork a;
char* error = nullptr;
if (sqlite3_exec(db, tQuery.c_str(), &artwork::populateFromDB, static_cast<void*>(&a), &error) != SQLITE_OK)
sqlite_free(error)
Or as a friend
function instead:
class artwork
{
friend int getArtwork(void*, int, char**, char**);
private:
std::string title;
std::string creator;
};
int getArtwork(void* object, int, char** data, char**)
{
if (artwork* const art = static_cast<artwork*>(object))
{
art->title = data[1];
art->creator = data[2];
}
return 0;
}
artwork a;
char* error = nullptr;
if (sqlite3_exec(db, tQuery.c_str(), &getArtwork, static_cast<void*>(&a), &error) != SQLITE_OK)
sqlite_free(error)