Sign in to follow this  
DrPepperCorn

Declaring a type in the same class

Recommended Posts

Hi there, been out of the programming thing for a while now and just recently got back into ot . . .totally forgot some principles but my books arent giving me a solution fast, even the net is not getting me any closer. I would like if you have the time an explanation into my problem and the lingo that goes with it I am trying to create a simple game where characters attack each other, you can choose froma list the character you want and then have it attack another using some choice commands. Here is my problem Ive made a class to hold the info about a character, cBaseChar, this has a method in it to set the char, SetChar(name, health, level) I have made an instance of this class and called it player one cBaseChar* playerone = new cBaseChar and set the set char, PlayerOne->("Ian", 100, 1). this all compiles nice. However i want to put these made chars in an array for now but cant declare an array in the class cBaseChar . . well i can if i go cBaseChar * Chararray[2]. When i try to put this player one into the array containing cBaseChars it wont let me saing converting void to cBaseChar* is not allowed. How would i go about doing this . . if this doesnt make any sense ill try to reword it. . . . thanks people

Share this post


Link to post
Share on other sites
[EDIT - I'm assuming C++. Just realised that might not be the case.]

I think we would benefit from seeing some code. I mean...


cBaseChar *Chars[2];

void f()
{
cBaseChar *C=new cBaseChar();

Chars[0]=C;

// ...

delete Chars[0];
}


shouldn't be causing any warnings about void to cBaseChar* conversions.

To be honest, unless the construction and copying of your cBaseChar's are expensive, it would be easier to not mess about with pointers and just push your cBaseChar's into a vector:


std::vector<cBaseChar> Chars;

void f()
{
cBaseChar C;
C.SetChar(10,20,"Eric");

Chars.push_back(C);
}


That creates a copy of the temporary character C in the vector.

And the other mandatory thing to point out is that your SetChar method would almost certainly be better expressed as the constructor for cBaseChar.


class cBaseChar
{
public:
cBaseChar(int S=0,std::string N="") : Strength(S),Name(N) { }
};

std::vector<cBaseChar> Chars;

void f()
{
Chars.push_back(cBaseChar(10,"Eric"));
}

Share this post


Link to post
Share on other sites
wow, that just looks mad, and im out of practice. Looks straight forward enough tho, very neat. i messed with this and getting it to compile but getting a null reference object or something or other. Could you explain why i would be getting this null pointing object and how i might stop it from happening in the future, that would be great, i hate it when i just dont understand why something is happning and finding thios information in a static book is quite hard i find. Even the searches ive done, im not sure technically what im asking.

could this be sorted at all?

Chararray[0]->PlayerOne->SetChar("Ian", 100, 1);

ive declared PlayerOne as a pointer to a cBaseChar in cBaseChar.

Than ks for the help . . now im getting motivated

[Edited by - DrPepperCorn on March 9, 2007 9:00:25 AM]

Share this post


Link to post
Share on other sites
Quote:

Chararray[0]->PlayerOne->SetChar("Ian", 100, 1);

Sounds like you are trying to use a NULL pointer.
Is PlayerOne pointing to valid memory?
Merrily declaring a pointer
cBaseChar *PlayerOne;
will leave you with a pointer pointing to invalid memory.

Share this post


Link to post
Share on other sites
As EasilyConfused pointed out, you'd need to create an instance of cBaseChar for the pointer to point to by using new. Simply creating a pointer does not make an instance.

As EasilyConfused also pointed out, it's much easier just to use a std::vector.

Quote:
Chararray[0]->PlayerOne->SetChar("Ian", 100, 1);

I'm assuming things haven't changed much from your original post:
Quote:
cBaseChar* playerone = new cBaseChar

Note that since playerone is a pointer to an instance of the class, and Chararray holds (what I assume to be) pointers to instances of the class, it would look something more like this:
Chararray[0]->SetChar("Ian", 100, 1);
I assume, of course, that the cBaseChar class does not have a member variable called "PlayerOne."

Also, might I suggest renaming these "character" class variables as something other than anything deriving from "char"? As it is, char has a pretty well-defined role in C++, and it's a bit confusing when using it to mean something else. [wink]

Best of luck!
-jouley

Share this post


Link to post
Share on other sites
You can make sure its valid memory by simply checking the pointer before using it:

if ( Chararray[0] )
Chararray[0]->PlayerOne->SetChar("Ian", 100, 1);

This however assumes that you set all of your pointers to NULL (0) when you declare them.

Share this post


Link to post
Share on other sites
Quote:
Original post by DrPepperCorn
woah . . heavy man


It certainly is [smile].

You really need to post some code, DrPepperCorn. We're shooting in the dark here.

Pop a minimal sample that exhibits the problem inside [ source ][ /source ] tags (without the spaces).

Share this post


Link to post
Share on other sites
Hi

If you want to create a array of characters in a class you have to rewrite it.
A class is an object but when created is an instance of a object if you really want to create an array of your objects you have to create a class that holds the arrays.

#include "cBaseChar.h"

class cBaseCharArray
{
private:
struct cBaseCharArrayitem
{
cBaseChar * charakter;
...
};

cBaseCharArrayItem * cBaseCharArrayList;


public:
void add(cBaseChar *);
cBaseChar * Item(int index);
...
};

Code is not perfect because the add should return a index
I hope you understand my meaning create is single list with a index
that you return when you seach, seach after the index
aso...

Have fun

Share this post


Link to post
Share on other sites
Quote:
Original post by Joran
#include "cBaseChar.h"
typedef std::vector<cBaseChar> cBaseCharArray;


Shortened, corrected and beautified [wink]

Usage is quite simple:

cBaseCharArray characters(2); // create an array of two characters
characters[0].doSomething(); // call member functions
characters.push_back(cBaseChar()); // Add new characters


And so on. Note that memory is handled for you, so you don't have to delete anything in this example.

Share this post


Link to post
Share on other sites
thanks for all the snippets, really im trying to understand why these errors are occuring and then i can get an explanation and prevent it happening in the future. As far as it goes i just want to create an array of class instances and select them and have them attack each other. When i get these nukll exceptions and stack overflowes it doesnt hwl[, i just want to know why im getting these and understand concepts before i start getting myself bogged down

thanks people u are a great help and shpowing me some new wayts of lookinbg at it

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this