Public Group

# Declaring a type in the same class

This topic is 4212 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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 on other sites
how would i retify?

##### 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 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 on other sites
now my compiler is giving me a fatal stack overflow error . . woah . . heavy man

##### Share on other sites
Quote:
 Original post by DrPepperCornwoah . . 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 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:
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

1. 1
2. 2
Rutin
23
3. 3
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 12
• 10
• ### Forum Statistics

• Total Topics
632579
• Total Posts
3007168

×