gpt4 book ai didi

c++ - 如何在 C++ 中为 sqlite 进行非静态回调?

转载 作者:行者123 更新时间:2023-11-30 01:35:04 26 4
gpt4 key购买 nike

我有一些代码可以从数据库中获取数据并将其存储在结构中,但我收到错误消息“必须调用对非静态成员函数的引用”。

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);

}
};

最佳答案

诀窍是创建一个静态“辅助”方法,并使用回调签名中提供的空指针参数将指向您的对象的指针传递到该方法中。然后静态辅助方法可以使用该指针来调用非静态方法:

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);

关于c++ - 如何在 C++ 中为 sqlite 进行非静态回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54848606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com