Tamara Caligari
Tamara Caligari

Reputation: 509

C++ return type does not match function type

I am getting an error 'return type does not match function type' when I am trying to return a pointer to a player in my function. The error from the compiler is:

'&' : illegal operation on bound member function expression

CPlayer* CLevel::getPlayer()
{
return &player;
}

In the header file, this is defined as:

private:
    CPlayer player(Point p, CGame* game);

public:
    CPlayer* getPlayer();

Any ideas as to why I am getting this error and how I can remove it?

EDIT:

Level constructor:

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 constructor:

 CPlayer::CPlayer(Point pos, CGame* game)
 {
this->game=game;
Point p;
p.x=0;
p.y=0;
setPosition(p);
 }

Upvotes: 1

Views: 10807

Answers (1)

juanchopanza
juanchopanza

Reputation: 227370

Because in your code, player is the name of a private function:

private:
    CPlayer player(Point p, CGame* game);

This is a function that takes a Point, a CGame*, and returns a CPlayer.

To declare a CPlayer data member you need

private:
    CPlayer player;

which you can then initialize in the constructor, for example:

CLevel(Point p, CGame* game) : player(p, game) {}

and:

CLevel() : player(Point(), game) {
  this->game = .... ;
}

Or, in C++11, you can do it like this:

private:
    CPlayer player{Point(), nullptr}; // I am not sure where you were getting p and game in your original example

Upvotes: 3

Related Questions