Page 1 of 1

C++ O.O.P HELP NEEDED!

Posted: Sun Apr 24, 2011 5:38 am
by VoidElite
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:

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
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? :roll:

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!

Re: C++ O.O.P HELP NEEDED!

Posted: Sun Apr 24, 2011 9:24 am
by N64vSNES
If this doesn't work:

Code: Select all

Npc *tom=new Npc(); //this doesn't(it should though because Npc is inheriting from Creature)
And this does:

Code: Select all

Creature *tom=new Creature(); //this works
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.

Re: C++ O.O.P HELP NEEDED!

Posted: Sun Apr 24, 2011 10:08 am
by XianForce
Your header guards aren't right... They should be like:

Code: Select all

#ifndef CLASS_NAME
#define CLASS_NAME

//class....

#endif
The define shouldn't come after the class.

Re: C++ O.O.P HELP NEEDED!

Posted: Sun Apr 24, 2011 12:04 pm
by VoidElite
XianForce wrote:Your header guards aren't right... They should be like:

Code: Select all

#ifndef CLASS_NAME
#define CLASS_NAME

//class....

#endif
The define shouldn't come after the class.
Well it works the way I'm doing it. It was just a typo in the Npc.h file header guards. I put

Code: Select all

#ifndef Npc
when I should have put

Code: Select all

#ifndef NPC
.

Re: C++ O.O.P HELP NEEDED!

Posted: Sun Apr 24, 2011 12:09 pm
by Ginto8
XianForce wrote:Your header guards aren't right... They should be like:

Code: Select all

#ifndef CLASS_NAME
#define CLASS_NAME

//class....

#endif
The define shouldn't come after the class.
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.

Re: C++ O.O.P HELP NEEDED!

Posted: Mon Apr 25, 2011 3:05 pm
by Boogy
Ginto8 wrote:
XianForce wrote:Your header guards aren't right... They should be like:

Code: Select all

#ifndef CLASS_NAME
#define CLASS_NAME

//class....

#endif
The define shouldn't come after the class.
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

Re: C++ O.O.P HELP NEEDED!

Posted: Mon Apr 25, 2011 4:52 pm
by dandymcgee
Boogy wrote:
Ginto8 wrote:
XianForce wrote:Your header guards aren't right... They should be like:

Code: Select all

#ifndef CLASS_NAME
#define CLASS_NAME

//class....

#endif
The define shouldn't come after the class.
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.

Re: C++ O.O.P HELP NEEDED!

Posted: Tue Apr 26, 2011 5:08 am
by Boogy
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

Re: C++ O.O.P HELP NEEDED!

Posted: Tue Apr 26, 2011 12:29 pm
by dandymcgee
Boogy wrote:
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.

Re: C++ O.O.P HELP NEEDED!

Posted: Tue Apr 26, 2011 1:34 pm
by VoidElite
dandymcgee wrote:
Boogy wrote:
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'.

Anywhoo here's 'Content.h':

Code: Select all

#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. :)