简体   繁体   中英

C++ illegal operation on bound member function

I had a problem with an illegal access error and I have removed the default constructor from Player.h as I deduced that the problem was due to it. The problem I am having now is that the Level.cpp wanted a default constructor so I edited the Level.h file as shown. That problem was resolved but now I am not being able to return a pointer to the player. The error 'illegal operation on bound member function' is being shown. Any ideas please? I'm a beginner in C++ and any help would be appreciated.

Player.h:

#ifndef _TAG_PLAYER
#define _TAG_PLAYER
#pragma once
#include "Tile.h"
#include "Point.h"

class CGame;
class CPlayer : public CTile
{

public:

CPlayer(Point pos, CGame* game);
~CPlayer();
char getDisplay() ;
virtual bool canMove(const Direction direction, Point p) ;
virtual void move(const Direction direction, Point p);
bool CheckForHome() ;
};
 #endif _TAG_PLAYER

Player.cpp:

#include "Box.h"
#include "Level.h"
#include "Tile.h"


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


CPlayer::~CPlayer()
{
}

bool CPlayer::CheckForHome() {

Point p = getPosition();
bool OnHomeTile;

if(game->getLevel()->getTiles()[p.y][ p.x] == GOAL)
{
    OnHomeTile = true;
} else {
    OnHomeTile = false;
}

return OnHomeTile;
}


char CPlayer::getDisplay()
{
if (CheckForHome())
{
    return SOKOBANONGOAL_CHAR;
}
else
{
    return PLAYER_CHAR;
}
}

Level.h:

 #pragma once
 #include "Point.h"
 #include "Tile.h"
 #include "Player.h"
 #include "Box.h"
 #include <list>
 #include <string>

 class CGame;

 class CLevel 
   {
    private:


list<CBox> boxes;
TileType tiles[GRID_HEIGHT][GRID_WIDTH];
CPlayer player(Point p, CGame* game);  -> new declaration
//CPlayer player;                      -> old declaration

 protected:
CGame* game;

 public:
CLevel();
~CLevel();

CPlayer* getPlayer();
list<CBox>* getBoxes();
TileType (*getTiles())[GRID_WIDTH];

};

Constructor of Level.cpp

  CLevel::CLevel()
  {
this->game=game;
Point p;
p.x=0;
p.y=0;
player(p,game);

memset(tiles, GROUND, sizeof(TileType)*GRID_HEIGHT*GRID_WIDTH);
}

The function with the error in Level.cpp:

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

Currently you've declared player as a member function not a member variable, which is why you're getting the weird error message. You can't mix the declaration and the initialisation of member variables like this.

You declaration should just be

CPlayer player;

But your CLevel constructor needs to initialise it, for example like:

CLevel() : player(Point(0, 0), game) { }

The problem with that though is that currently CLevel doesn't have a game to initialise the player with. Perhaps you could pass the game to the constructor of CLevel ?

I think you need to read up on constructors and initialisation of members a bit more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM