Ugh, I havn't posted a question in forever...

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Ugh, I havn't posted a question in forever...

Post 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???
Image
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Ugh, I havn't posted a question in forever...

Post by LeonBlade »

Did you set the length of the array?

(wrote you array by mistake lol)
Last edited by LeonBlade on Sat Jan 31, 2009 9:11 am, edited 1 time in total.
There's no place like ~/
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Ugh, I havn't posted a question in forever...

Post 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...
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: Ugh, I havn't posted a question in forever...

Post by Arce »

Can you show us where you initia1ize the array?
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Ugh, I havn't posted a question in forever...

Post by M_D_K »

I'm pretty sure thats it at the bottom of the code snippet.

Code: Select all

} e[2];
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: Ugh, I havn't posted a question in forever...

Post by Trask »

M_D_K wrote:I'm pretty sure thats it at the bottom of the code snippet.

Code: Select all

} e[2];

Well you never defined 'e' from what I see right? You'd have to have something like: int e[2]; before using the array.
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Ugh, I havn't posted a question in forever...

Post 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.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: Ugh, I havn't posted a question in forever...

Post 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. :shock:
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Ugh, I havn't posted a question in forever...

Post 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...
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Ugh, I havn't posted a question in forever...

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Ugh, I havn't posted a question in forever...

Post 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...
Image
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Ugh, I havn't posted a question in forever...

Post 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
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Ugh, I havn't posted a question in forever...

Post 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... :shock:
Image
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Ugh, I havn't posted a question in forever...

Post 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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Ugh, I havn't posted a question in forever...

Post 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...
Image
Post Reply