Page 1 of 2
Ugh, I havn't posted a question in forever...
Posted: Fri Jan 30, 2009 11:24 pm
by eatcomics
So I'm doing a text based rpg in c++, and I'm working on the battle system, well the problem is I have a class
Code: Select all
class Enemy {
string nam; //name of said enemy
int chp; //current hp
int cmp; //current mp
int att; //attack
int def; //defense
int res; //resistance
int spe; //speed
int pre; //precision
int crt; //critical
public:
string ENam(string);
int EChp(int,int,int,int);
int ECmp(int,int,int,int);
int EAtt(int,int,int,int);
int EDef(int,int,int,int);
int ERes(int,int,int,int);
int ESpe(int,int,int,int);
int EPre(int,int,int,int);
int ECrt(int,int,int,int);
} e[2];
well the problem is the array of enemies I have set up doesn't work, it will only let me use the first variable e[0] and then if I try to use any of the others it crashes, it compiles but the game crashes... and that makes me sad...
Can anyone help me with this problem???
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 1:08 am
by LeonBlade
Did you set the length of the array?
(wrote you array by mistake lol)
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 7:30 am
by programmerinprogress
I built a small test program(in the console), using and array declaration at the end of the class, and I was able to access and change it's members...
I then ran it through my engine'ish thing(so I could see the objects visually), and I managed to draw two boxes to the screen when I defined an array of [2] after the declaration, it didn't work at first because it was split into files and I was getting a multiple redefinition error (but declaring the array of objects as static fixed that, since I'll only need 1 instance anyway, I don't want multiple definitions)
So i'm thinking your problem may lie somewhere outside that function, from what I can see, I can't spot anything that should offend the compiler...
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 9:03 am
by Arce
Can you show us where you initia1ize the array?
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 9:26 am
by M_D_K
I'm pretty sure thats it at the bottom of the code snippet.
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 10:53 am
by Trask
M_D_K wrote:I'm pretty sure thats it at the bottom of the code snippet.
Well you never defined 'e' from what I see right? You'd have to have something like: int e[2]; before using the array.
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 11:01 am
by M_D_K
programminginprogress said it works and declaring like that should work. I'd never do it, I'd declare that enemy list inside level or something. Cause if memory serves me right(I've been on an all nighter...again). Declaring like that makes it global, which you don't need.
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 11:04 am
by Trask
So you're saying that the type of array it is, is the class? as in: Enemy e[2]; is the declaration essentially, thus allowing access to the variables in the class into the array?
Never did that one before.
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 11:19 am
by M_D_K
I've seen it used with structs in C. And it followed into C++. The OO nazis hate it. I just don't use it cause it looks tacky(I like clean code
).
Anyway to answer your question yes it is just doing
Enemy e[2];
I think...
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 1:16 pm
by MarauderIIC
But in C structs, it just defines the actual name for the struct, right? Thought it was like a typedef if at the end of the struct.
Also please do yourself a favor and label your parameter names.
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 1:19 pm
by eatcomics
Well, let me spend a little time with it... I'm gonna try and figure out if maybe it's when I set all the variables for the objects...
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 2:19 pm
by M_D_K
It can also define arrays. I'm pretty sure I saw this somewhere
*dramatic reconstruction*
Code: Select all
struct Vertex
{
float x, y;
unsigned int colour;
} Triangle[3];
Me thinks PSP code somewhere.
I'm running on empty so some shit might be wrong. But I'm pretty sure thats right
Re: Ugh, I havn't posted a question in forever...
Posted: Sat Jan 31, 2009 6:02 pm
by eatcomics
I just changed it from a class to a struct, and all is well now... It should be okay like that I think...
Re: Ugh, I havn't posted a question in forever...
Posted: Sun Feb 01, 2009 1:08 pm
by programmerinprogress
I don't actually see how that changes anything (I mean I tried both struct and class and they both worked)
As far as I know, a C++ struct (as in a one which allows methods and members to be mixed) is essentially the same as the class (with the exception that structs are public by default, and classes are private by default)
But if it works, it works, I would just keep an eye on it though.
Re: Ugh, I havn't posted a question in forever...
Posted: Sun Feb 01, 2009 2:13 pm
by eatcomics
programmerinprogress wrote:I don't actually see how that changes anything (I mean I tried both struct and class and they both worked)
As far as I know, a C++ struct (as in a one which allows methods and members to be mixed) is essentially the same as the class (with the exception that structs are public by default, and classes are private by default)
But if it works, it works, I would just keep an eye on it though.
I wouldn't think it would have an effect either but it did, or so I thought, I think it was just when I was declaring the variables something is wrong, I'll look into it somemore if I need help I'll post but I think I can get it working...