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:
//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
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!
Last edited by VoidElite on Sun Apr 24, 2011 9:55 am, edited 1 time in total.
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).
I settling into my hybrid(procedural&object orientated) life. It's all good so far.
Something must be going wrong with including the NPC header file.
I would suggest making sure you aren't including it multiple times in different places but that's all I can think of off the top of my head.
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).
I settling into my hybrid(procedural&object orientated) life. It's all good so far.
This is actually nothing more than a matter of preference. You could put the #define smack in the middle of the class, as long as it's between the #ifndef and the #endif. However, I think it's a fairly common convention to have the #define right after the #ifndef.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
This is actually nothing more than a matter of preference. You could put the #define smack in the middle of the class, as long as it's between the #ifndef and the #endif. However, I think it's a fairly common convention to have the #define right after the #ifndef.
You can also use #pragma once at the top of a header file.
#pragma once is faster then a #ifndef. Because #ifndef requires the c preprocessor to scan the file
This is actually nothing more than a matter of preference. You could put the #define smack in the middle of the class, as long as it's between the #ifndef and the #endif. However, I think it's a fairly common convention to have the #define right after the #ifndef.
You can also use #pragma once at the top of a header file.
#pragma once is faster then a #ifndef. Because #ifndef requires the c preprocessor to scan the file
That's fine, just keep in mind that #pragma is not supported by every compiler.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
dandymcgee wrote:That's fine, just keep in mind that #pragma is not supported by every compiler.
True, but if you still use a compiler that doesn't have support for #pragma you really need to upgrade :P
It only really matters if you're one of those people making the next great game engine (seems like that's everyone's current project), in which case someone is SURE to attempt to compile it on Linux eventually. #pragma is not a standard at all.. just because a compiler doesn't support it doesn't make that compiler out-of-date by any means.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
dandymcgee wrote:That's fine, just keep in mind that #pragma is not supported by every compiler.
True, but if you still use a compiler that doesn't have support for #pragma you really need to upgrade :P
It only really matters if you're one of those people making the next great game engine (seems like that's everyone's current project), in which case someone is SURE to attempt to compile it on Linux eventually. #pragma is not a standard at all.. just because a compiler doesn't support it doesn't make that compiler out-of-date by any means.
Wait!
I have another few errors. Now that I've fixed that I'm getting 'screen' has not been declared in this scope but 'screen' is declared in 'Content.h' which is being included into 'Sprite.h'.
#ifndef CONTENT
#define CONTENT
#include <iostream>
#include "SDL\SDL.h"
#include "windows.h"
#include "Environment.h"
#include "Sprite.h"
#include "Tile.h"
#include "Map.h"
#include "World.h"
#include "Object.h"
#include "Creature.h"
#include "Npc.h"
#include "Player.h"
using namespace std;
int mapIndex=0;
SDL_Surface *screen=NULL; //SDL's screen
Sprite *sprite=new Sprite("dude.bmp",10,10,true); //test sprite
Player *player=new Player(); //Main Player
World *world=new World(); //the world
#endif
Help once again would be greatly appreciated.
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).
I settling into my hybrid(procedural&object orientated) life. It's all good so far.