Class Dependency issues (C++)
Posted: Tue May 17, 2011 11:04 am
Language: C++
Compiler: GCC 4.4
I have a class NPC that contains an NPCBehavior* instance, the NPCBehavior class has a method update(NPC* npc). Both the classes are dependent on each other. I also have a DefaultNPCBehavior class that is used by the NPC class inside the constructor to give a default behavior to itself. DefaultNPCBehavior extends NPCBehavior. gcc is giving me the following message:
NPC.cpp
NPCBehavior.h
NPCBehavior.cpp
DefaultNPCBehavior.h
DefaultNPCBehavior.cpp
Compiler: GCC 4.4
I have a class NPC that contains an NPCBehavior* instance, the NPCBehavior class has a method update(NPC* npc). Both the classes are dependent on each other. I also have a DefaultNPCBehavior class that is used by the NPC class inside the constructor to give a default behavior to itself. DefaultNPCBehavior extends NPCBehavior. gcc is giving me the following message:
NPC.hIn file included from NPC.h:10,
from NPCBehavior.h:7:
DefaultNPCBehavior.h:10: error: expected class-name before ‘{’ token
Code: Select all
#ifndef NPC_H
#define NPC_H
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <iostream>
#include "Entity.h"
#include "AnimatedEntity.h"
#include "NPCBehavior.h"
#include "DefaultNPCBehavior.h"
class NPCBehavior;
class DefaultNPCBehavior;
class NPC : public AnimatedEntity {
private:
int mapShiftX, mapShiftY;
NPCBehavior* behavior;
public:
NPC(SDL_Surface* surface, int x, int y, int width, int height);
~NPC();
int getMapShiftX();
int getMapShiftY();
void setMapShiftX(int mapShiftX);
void setMapShiftY(int mapShiftY);
void setNPCBehavior(NPCBehavior* behavior);
void update(); //inherited from Entity, AnimatedEntity
void render(SDL_Surface* screen); //inherited from Entity, AnimatedEntity
};
#endif
Code: Select all
#include "NPC.h"
using namespace std;
NPC::NPC(SDL_Surface* sheet, int x, int y, int width, int height) : AnimatedEntity(sheet, x, y, width, height), mapShiftX(0), mapShiftY(0) {
behavior = new DefaultNPCBehavior();
cout << behavior << endl;
}
NPC::~NPC() {
//possible memory leak with behavior (WARNING)
}
void NPC::setNPCBehavior(NPCBehavior* behavior) {
delete this->behavior;
this->behavior = behavior;
}
int NPC::getMapShiftX() {
return mapShiftX;
}
int NPC::getMapShiftY() {
return mapShiftY;
}
void NPC::setMapShiftX(int mapShiftX) {
this->mapShiftX = mapShiftX;
}
void NPC::setMapShiftY(int mapShiftY) {
this->mapShiftY = mapShiftY;
}
void NPC::update() {
AnimatedEntity::update();
setX(getX()+getXVel());
setY(getY()+getYVel());
behavior->update(this);
}
//extremely similar to AnimatedEntity::render with the change being with the offset rectangle
void NPC::render(SDL_Surface* screen) {
SDL_Rect offset;
offset.x = getX()+mapShiftX;
offset.y = getY()+mapShiftY;
SDL_Rect clip;
clip.x = getFrameNum()*getWidth();
clip.y = getDirection()*getHeight();
clip.w = getWidth();
clip.h = getHeight();
SDL_BlitSurface(getSheet(), &clip, screen, &offset);
}
Code: Select all
#ifndef NPCBEHAVIOR_H
#define NPCBEHAVIOR_H
#include <iostream>
#include "NPC.h"
#include "Entity.h"
class NPC;
class NPCBehavior {
public:
NPCBehavior();
virtual void update(NPC* npc) = 0;
};
#endif
Code: Select all
#include "NPCBehavior.h"
NPCBehavior::NPCBehavior() {
}
Code: Select all
#ifndef DEFAULTNPCBEHAVIOR_H
#define DEFAULTNPCBEHAVIOR_H
#include "Entity.h"
#include "NPCBehavior.h"
#include "NPC.h"
class NPC;
class DefaultNPCBehavior : public NPCBehavior {
private:
int counter;
public:
DefaultNPCBehavior();
void update(NPC* npc);
};
#endif
Code: Select all
#include "DefaultNPCBehavior.h"
DefaultNPCBehavior::DefaultNPCBehavior() : counter(0) {
}
void DefaultNPCBehavior::update(NPC* npc) {
if(npc->getXVel() == 0) npc->setXVel(4);
counter++;
if(counter == (60*4)) {
counter = 0;
npc->setXVel(-npc->getXVel());
}
}