私は C++ で Pacman ゲームに取り組んでいますが、メンバー関数ポインターで問題が発生しました。から継承する2 つのクラスがpacman
あります。サブクラスでは、関数を の関数に渡す必要があります。ただし、静的変数が必要になるため、単純に静的関数を使用することはできません。これは機能しません。ghost
Mouvement
Mouvement
&this->movingUp
「メンバー関数への非定数ポインターを作成できません」というエラーをスローするパスを試しました
「タイプ 'void ( )(int)' の右辺値でタイプ 'void (:: )(int)'&<ghost or pacman>::movingUp
のパラメーターを初期化できません」というエラーをスローするパスを試しました。
関連するものは次のとおりです: (この問題に必要なものだけが表示されるように、ほとんどを切り取っています)
class cMouvement {
protected:
int curDirection = -3; // Variables that are used in the 'movingUp, etc' functions.
int newDirection = -3; // And therefore can't be static
public:
void checkIntersection(void (*function)(int), bool shouldDebug){
// Whole bunch of 'If's that call the passed function with different arguments
}
そして、この時点では非常によく似ているclasspacman
とです。ghost
class pacman : public cMouvement {
void movingUp(int type){
// Blah blah blah
}
// movingDown, movingLeft, movingRight... (Removed for the reader's sake)
public:
/*Constructor function*/
void move(bool shouldDebug){
if (curDirection == 0) {checkIntersection(&movingUp, false);}
else if (curDirection == 1) {checkIntersection(&movingRight, false);}
else if (curDirection == 2) {checkIntersection(&movingDown, false);}
else if (curDirection == 3) {checkIntersection(&movingLeft, false);}
}
};