簡體   English   中英

C ++函數調用作為參數,不起作用

[英]C++ Function call as a parameter, not working

http://pastebin.com/CsViwQFg我使用的是稱為DragonFireSDK的SDK,並且有一個名為TouchAdd()的函數,可以讓我添加一個函數作為參數(在本例中為MoveLeft()和MoveRight())。 唯一的問題是,如果函數在一個類(在本例中為Player類)中,則會出現以下錯誤:

Player *player;

void AppMain()
{
    player = new Player(20,20,10);

    tleft = TouchAdd(0,0,180,480,player->MoveLeft,0);
    tright = TouchAdd(180,0,180,480,player->MoveRight,0);
}

錯誤:

error C3867: 'Player::MoveLeft': function call missing argument list; use '&Player::MoveLeft' to create a pointer to member
error C3867: 'Player::MoveRight': function call missing argument list; use '&Player::MoveRight' to create a pointer to member

如果要將函數作為參數傳遞,則語法為&Player::MoveLeft; 因為它沒有綁定到任何對象(例如player

DragonFireSDK似乎需要“ C”可調用函數,並且您正在嘗試傳遞成員函數(盡管未使用正確的語法)。 我認為您需要執行以下操作:

Player *player;

extern "C"
int PlayerMoveLeft(int id, int event, int x, int y)
{
    // do something - I'm not sure what might be possible 
    //  to get a pointer or a reference to the player object
    //  hopefully one or more parameters passed to this callback
    //  will have the information you need to do that

    // or if you only have one global player, you're set - 
    //  just use it
    Player* player = /* ??? */;

    player->MoveLeft( id, event, x, y); // or whatever needs to be passed

    return 0;
}

extern "C"
int PlayerMoveRight(int id, int event, int x, int y)
{
    Player* player = /* ??? */;

    player->MoveRight( id, event, x, y); // or whatever needs to be passed

    return 0;
}


void AppMain()
{
    player = new Player(20,20,10);

    tleft = TouchAdd(0,0,180,480,PlayerMoveLeft,0);
    tright = TouchAdd(180,0,180,480,PlayerMoveRight,0);
}

請注意,即使靜態成員函數通常可以正常工作(由於沒有傳入this指針的“隱藏”,嚴格來說,您應該使用非成員extern "C"函數。

由於TouchAdd的功能簽名(從此處獲取 )為

int TouchAdd(int x, int y, int width, int height, int (*callback)(int id, int event, int x, int y), int id);

預期函數必須是自由函數,例如:

int myCallback(int id, int event, int x, int y){
  // do your stuff
}

void AppMain(){
  tLeft = TouchAdd(....,&myCallback,...);
}

您不能傳遞成員函數指針( &Player::MoveX ),因為該函數需要在該類的對象( Player )上調用。 因此,您需要為此使用一種解決方法:

Player* player;

int PlayerMoveLeft(int id, int event, int x, int y){
  return player->MoveLeft(id,event,x,y);
}

int PlayerMoveRight(int id, int event, int x, int y){
  return player->MoveRight(id,event,x,y);
}

void AppMain(){
  player = new Player(20,20,10);
  tLeft = TouchAdd(...,&PlayerMoveLeft,...);
  tRight = TouchAdd(...,&PlayerMoveRight,...);
}

}

似乎id是傳遞給回調的自定義參數。 如果您只有32位目標(並且DragonFireSDK似乎僅適用於iPhone,那么我想答案是肯定的),則可以將其強制轉換為Player *以綁定到播放器實例。

int PlayerMoveLeft(int id, int event, int x, int y)
{
    Player* player = reinterpret_cast<Player*>(id);

    return player->MoveLeft(event, x, y);
}

int PlayerMoveRight(int id, int event, int x, int y)
{
    Player* player = (Player*)id;

    return player->MoveRight(event, x, y);
}

void AppMain()
{
    Player* player = new Player(20,20,10);

    tleft = TouchAdd(0,0,180,480,PlayerMoveLeft,(int)player);
    tright = TouchAdd(180,0,180,480,PlayerMoveRight,(int)player);
}

即使這行不通,或者您不想使用某種類型的強制類型轉換,也始終可以使用帶有查找表的全局或靜態對象。 使Player類的PlayerMoveLeft和PlayerMoveRight靜態成員看起來也更好,並且我認為它應該與TouchAdd()一起玩。

tleft = TouchAdd(0,0,180,480,player->MoveLeft,0);
tright = TouchAdd(180,0,180,480,player->MoveRight,0);

您沒有將參數傳遞給MoveLeftMoveRight函數。 我想它們是函數調用,就像您的主題的標題所說的那樣,因此,如果它們接受參數,則也必須傳遞參數。

如果他們不接受爭論,那就這樣做:

tleft = TouchAdd(0,0,180,480,player->MoveLeft(),0);
tright = TouchAdd(180,0,180,480,player->MoveRight(),0);

如果它們不是函數調用,則您要傳遞成員函數指針,然后執行以下操作:

tleft = TouchAdd(0,0,180,480, &Player::MoveLeft,0);
tright = TouchAdd(180,0,180,480, &Player::MoveRight,0);

您還需要傳遞實例,以便以后可以調用成員函數。

如果您讓我們知道TouchAdd功能的簽名, TouchAdd 這樣我們就可以更具體地回答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM