Hi,
I have a base class character, a child class pacman, and a file called manager.cpp which contains main and serves to manage the characters in the game while the game is in progress.
In Character.h I have declared desiredDirection and its mutators and accessors as follows:
Code:
protected:
void setDirection(int newDirection);
const int getDirection() const;
void setDesired(int newDirection); //set desired direction
const int getDesired() const;
...
private:
...
int direction; //current direction
int desiredDirection;
...
In manager.cpp I have declared pacman to be a global variable as follows (I know global variables are naughty but I haven't thought of the best way to manage the characters yet so its temporary):
Code:
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <string> //might be able to remove later - here for debugging
#include "Map.h"
#include "Pacman.h"
#include "Ghost.h"
using namespace std;
Pacman * pacman;
in the same file (manager.cpp), I need to access pacman's desiredDirection and I do it as follows:
Code:
void Timer(int extra)
{
if (pacman->getDesired() == 4)
pacman->move();
else
{
pacman->move();
pacman->setDesired(pacman->move(pacman->getDesired()));
}
glutTimerFunc(30,Timer,0);
}
before I had this working by having desiredDirection as a global variable in Manager.cpp but I'm trying to avoid using global variables. When I compile my program I get:
manager.cpp(43) : error C2248: 'Character::getDesired' : cannot access protected member declared in class 'Character'
character.h(38) : see declaration of 'Character::getDesired'
character.h(15) : see declaration of 'Character'
I'm guessing the only way I can use the getters and setters is if I used them within pacman's implementation file? So, if I need access in manager.cpp, what would be the best way to do it?