C++ O.O.P HELP NEEDED!
Posted: Sun Apr 24, 2011 5:38 am
I would say I'm quite new to O.O.P in C++ as I'm more used to C#, Java and Assembly language. You see I decided to start working on a OpenGL Game Engine for Windows so I realised that the best thing I could do is set up the main architecture of the engine. So I've created a Main.cpp and a load of random headers and I was testing the functionality when I encountered a really irritating error which I just couldn't get myself to fix. Hope you guys can help:
The error says that tom(the Npc instance) is undeclared which it isn't because it's declared in the main function of Main.cpp.
Help?
EDIT: as you've probably noticed I haven't included ALL the code from the engine because it is not necessary, all you need to know is that Npc is inheriting from Creature and Creature is inheriting from Sprite. Like I said though instantiating an instance of Creature and then accessing name from it works but doesn't work from Npc.
RESOLVED!
Code: Select all
//Main.cpp
#include <iostream>
#include "Map.h"
#include "Object.h"
#include "Player.h"
#include "Npc.h"
using namespace std;
int main(int argc,char *argv[]){
Npc *tom=new Npc(); //this doesn't(it should though because Npc is inheriting from Creature)
//Creature *tom=new Creature(); //this works
tom->name="tomx";
cout<<"hi "<<tom->name<<endl;
system("pause");
}
//Npc.h
#include <iostream>
#include "Creature.h"
using namespace std;
#ifndef Npc
class Npc:public Creature{
public:
Npc();
Npc(string name2,int hp2,int attack2,int strength2,int defense2,int show2);
};
Npc::Npc(){
name="Randomer";
hp=0;
attack=0;
strength=0;
defense=0;
show=false;
alive=true;
}
Npc::Npc(string name2,int hp2,int attack2,int strength2,int defense2,int show2){
name=name2;
hp=hp2;
attack=attack2;
strength=strength2;
defense=defense2;
show=show2;
alive=true;
}
#define Npc
#endif
//Creature.h
#include <iostream>
#include "Sprite.h"
using namespace std;
#ifndef CREATURE
class Creature:public Sprite{
public:
Creature();
Creature(string name2,int hp2,int attack2,int strength2,int defense2,int show2);
string name;
int hp;
int attack;
int strength;
int defense;
bool alive;
};
Creature::Creature(){
name="Randomer";
hp=0;
attack=0;
strength=0;
defense=0;
show=false;
alive=true;
}
Creature::Creature(string name2,int hp2,int attack2,int strength2,int defense2,int show2){
name=name2;
hp=hp2;
attack=attack2;
strength=strength2;
defense=defense2;
show=show2;
alive=true;
}
#define CREATURE
#endif
Help?
EDIT: as you've probably noticed I haven't included ALL the code from the engine because it is not necessary, all you need to know is that Npc is inheriting from Creature and Creature is inheriting from Sprite. Like I said though instantiating an instance of Creature and then accessing name from it works but doesn't work from Npc.
RESOLVED!