Reputation:
I have some code that gets data from a database and stores it in a struct but I get an error "reference to non-static member function must be called".
class ScoreManagement {
private:
int callback(void *NotUsed, int argc, char **argv,
char **azColName) {
NotUsed = 0;
playerRecords[player_number].name = argv[1] ? argv[1] : "NULL";
player_number++
return 0;
}
void showScore(string userinput) {
string query = "SELECT * FROM SCORES";
sqlite3_exec(DB, query.c_str(), callback, NULL, NULL);
}
};
Upvotes: 0
Views: 896
Reputation: 73121
The trick is to a create a static "helper" method, and use the void-pointer argument provided in the callback-signature to pass a pointer to your object into that method. Then the static helper method can use that pointer to call the non-static method:
class ScoreManagement {
public:
static int callback(void *objPtr, int argc, char **argv, char **azColName) {
return ((ScoreManagement *)objPtr)->callbackAux(argc, argv, azColName);
}
private:
int callbackAux(int argc, char ** argv, char ** azColName) {
playerRecords[player_number].name = argv[1] ? argv[1] : "NULL";
player_number++;
return 0;
}
};
[...]
// Note that you must pass in a pointer to your `ScoreManagement` object
// as an argument here, so that it will be passed in to `callback()`
sqlite3_exec(DB, query.c_str(), ScoreManagement::callback, &myScoreManagementObject, NULL);
Upvotes: 4