我有一些代码可以从数据库中获取数据并将其存储在结构中,但出现错误“必须调用对非静态成员函数的引用”。
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);
}
};
答案 0 :(得分:3)
诀窍是创建一个静态的“ helper”方法,并使用回调签名中提供的void-pointer参数将指向您对象的指针传递到该方法中。然后,静态帮助器方法可以使用该指针来调用非静态方法:
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);