[英]C++ return type does not match function type
當我試圖在我的函數中返回指向播放器的指針時,我收到錯誤'返回類型與函數類型不匹配'。 編譯器的錯誤是:
'&':綁定成員函數表達式的非法操作
CPlayer* CLevel::getPlayer()
{
return &player;
}
在頭文件中,這被定義為:
private:
CPlayer player(Point p, CGame* game);
public:
CPlayer* getPlayer();
有關為什么我收到此錯誤以及如何刪除它的任何想法?
編輯:
關卡構造函數:
CLevel::CLevel()
{
Point p;
this->game=game;
p.x=0;
p.y=0;
player(Point p, CGame* game) {};
memset(tiles, GROUND, sizeof(TileType)*GRID_HEIGHT*GRID_WIDTH);
}
Player.cpp構造函數:
CPlayer::CPlayer(Point pos, CGame* game)
{
this->game=game;
Point p;
p.x=0;
p.y=0;
setPosition(p);
}
因為在您的代碼中, player
是私有函數的名稱:
private:
CPlayer player(Point p, CGame* game);
這是一個采用Point
, CGame*
並返回CPlayer
。
聲明您需要的CPlayer
數據成員
private:
CPlayer player;
然后,您可以在構造函數中初始化,例如:
CLevel(Point p, CGame* game) : player(p, game) {}
和:
CLevel() : player(Point(), game) {
this->game = .... ;
}
或者,在C ++ 11中,您可以這樣做:
private:
CPlayer player{Point(), nullptr}; // I am not sure where you were getting p and game in your original example
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.