Page 1 of 1
Item class
Posted: Mon May 03, 2010 8:00 pm
by Bullet Pulse
So, I was thinking of ways to implement an Item class in my project.
Each item object will belong to a group (hp, money, etc), have a modifier value (5, 10, 15) and have a name (goldBar, coin, etc).
The group, modifier value, and name affect how the item is used in the use method.
For example, the Player object will be modified in Z way by Y much.
Method 1 : Item class will contain a static pointer to a Player object,
which can be modified directly though mutator methods called on the player object by item objects.
Method 2: The Player class will have its own method for using Item objects.
How do you think I should do it?
You can suggest a different method if you'd like.
Re: How to prevent infinite inclusion?
Posted: Mon May 03, 2010 8:10 pm
by Live-Dimension
That may just work? Have you actually tried it?
If not, there may be a #pragma but I wouldn't count on it. You'll have to redo your classes to prevent the recursion.
Re: How to prevent infinite inclusion?
Posted: Mon May 03, 2010 8:14 pm
by Bullet Pulse
Live-Dimension wrote:That may just work? Have you actually tried it?
If not, there may be a #pragma but I wouldn't count on it. You'll have to redo your classes to prevent the recursion.
Sorry, I changed the topic to something that will actually prevent the infinite inclusion. So please take a look at it
And yes have tried it.
Re: Item class
Posted: Mon May 03, 2010 8:41 pm
by avansc
try this, create a header called something like, dependancies.h
then in this, declare all class names first, then include all the headers next.
now, just include dependancies.h in all the other headers.
this should solve your problem.
also, you need header guards in all your headers.
should look something like this.
Code: Select all
//dependancies.h
#ifndef _dependancies_h
#define _dependancies_h
class jew;
class shoe;
class kangaroo;
#include "jew.h"
#include "shoe.h"
#include "kangaroo.h"
#endif
Re: Item class
Posted: Mon May 03, 2010 8:43 pm
by Ginto8
Bullet Pulse wrote:So, I was thinking of ways to implement an Item class in my project.
Each item object will belong to a group (hp, money, etc), have a modifier value (5, 10, 15) and have a name (goldBar, coin, etc).
The group, modifier value, and name affect how the item is used in the use method.
For example, the Player object will be modified in Z way by Y much.
Method 1 : Item class will contain a static pointer to a Player object,
which can be modified directly though mutator methods called on the player object by item objects.
Method 2: The Player class will have its own method for using Item objects.
How do you think I should do it?
You can suggest a different method if you'd like.
The way I see it, it would probably be best to have an item function taking a parameter of a player class.
Re: Item class
Posted: Mon May 03, 2010 8:46 pm
by eatcomics
avansc wrote:try this, create a header called something like, dependancies.h
then in this, declare all class names first, then include all the headers next.
now, just include dependancies.h in all the other headers.
this should solve your problem.
should look something like this.
Code: Select all
//dependancies.h
#ifndef _dependancies_h
#define _dependancies_h
class jew;
class shoe;
class kangaroo;
#include "jew.h"
#include "shoe.h"
#include "kangaroo.h"
#endif
that's what I do, works wonders for the life of a programmer :D
well actually I don't put classes in there
Re: Item class
Posted: Mon May 03, 2010 9:03 pm
by avansc
eatcomix, you should, because if you dont declare the classes, you can run into issues where the header guards cause issues
i took the time to illustrate the issue
deps.h
Code: Select all
/*
* deps.h
* header
*
* Created by Andre van-Schalkwyk on 5/3/10.
* Copyright 2010 N/A. All rights reserved.
*
*/
#ifndef _deps_h
#define _deps_h
/*class a;
class b;*/
#include "a.h"
#include "b.h"
#endif
a.h
Code: Select all
/*
* a.h
* header
*
* Created by Andre van-Schalkwyk on 5/3/10.
* Copyright 2010 N/A. All rights reserved.
*
*/
#ifndef _a_h
#define _a_h
#include "deps.h"
class a
{
public:
private:
b *bclass;
};
#endif
b.h
Code: Select all
/*
* b.h
* header
*
* Created by Andre van-Schalkwyk on 5/3/10.
* Copyright 2010 N/A. All rights reserved.
*
*/
#ifndef _b_h
#define _b_h
#include "deps.h"
class b
{
public:
private:
a *aclass;
};
#endif
if you compile this you'll see it bitches at you that b is not declared.,
if you uncomment the class declarations it will compile fine.
anyways, just something to take note of.
Re: Item class
Posted: Mon May 03, 2010 9:55 pm
by eatcomics
yeah, I realized that after a while of getting problems with different files calling others that... ah whatever, its just a big pain if you don't