Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Using Arrays To Store Classe's

Author
Message
Fusspawn
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 21st Mar 2008 22:48
Ive been trying to store Classes in an array by doing something along the lines of



I know Other Programming languages have this ability Yet i can find nothing on how to store Class's in an array via google :/

Any help ?

Stuck in the land of the confused! and loving it!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 22nd Mar 2008 01:15
In C/C++ you must always precede the name (identifier) of the array with the data type. I'm not sure what the Player MainPlayer() thing is you're going for in your example but let's say your class name is Player. You would do the following:



The constructor will be applied to each element of the array as the array is created.

Lilith, Night Butterfly
Fusspawn
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 22nd Mar 2008 01:16
Your a star !

Stuck in the land of the confused! and loving it!
Fusspawn
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 22nd Mar 2008 01:17
the player MainPlayer(); was an example Player being the class name MainPlayer(); being the class constructor.

Stuck in the land of the confused! and loving it!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 22nd Mar 2008 01:18
I blush!

Lilith, Night Butterfly
Fusspawn
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 22nd Mar 2008 01:23
Hmm, After trying to Create an array to hold my player classes using

Player ArrayName[1]; Returns


but i can create a player using the same class using
Player MainPlayer();

What do i need to change in the constructor function to allow storage of the class in an array ?

Stuck in the land of the confused! and loving it!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 22nd Mar 2008 01:29
Quote: "the player MainPlayer(); was an example Player being the class name MainPlayer(); being the class constructor"


You can't have a constructor with a different name than the class name. When you instantiate a class object like:


an object name myPlayer will be created and at that time it will automatically run the constructor, a function with the name Player() will be run to set up the initial values per the constructor's code.

Sometimes you'll see someone do something along the lines of:



Which basically creates a Player object in the program's freestore (heap) and assigns its location to the pointer called myPlayer. From that point on you have to use the pointer to member notation (-> to reference any of the variables or methods.

Lilith, Night Butterfly
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 22nd Mar 2008 01:30
Quote: "What do i need to change in the constructor function to allow storage of the class in an array ?"


You need to name the constructor "Player()".

Lilith, Night Butterfly
Fusspawn
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 22nd Mar 2008 01:40
Its already called Player:layer();

I can use it in a normal manner.
Its just trying to store that in an array was causing issue's.

the idea is create a master array of all the players so i can access there member functions easily

Ie loop through the array and call

Arrayname[ID].Move();

Ill go try the pointer method,

thanks for the help its been bugging me and a freind. and without it we cant really do any more work

Stuck in the land of the confused! and loving it!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 22nd Mar 2008 01:58
I wouldn't recommend the pointer method. It was just a way of demonstrating a method that uses something similar to what you were initially showing.

I'm not sure why you're getting the error. Usually if you don't have a default constructor defined it will create one for you that does a minimal amount of initializing. It may only warn you that one isn't defined to alert you to something you might want to do yourself.

I may not have a clear idea of what you've developed so I don't know exactly where your problem lies.

Lilith, Night Butterfly
Rye
21
Years of Service
User Offline
Joined: 30th May 2003
Location: United Kingdom, Blackrod
Posted: 22nd Mar 2008 02:13
Player.h

Player.cpp



then where you need it:
Player PlayerArray[5];

That SHOULD be all you need. make sure that the constructor is public.
Fusspawn
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 22nd Mar 2008 02:13 Edited at: 22nd Mar 2008 02:33
Ive Tried using a blank contructor function

Player:layer()
{
}

But that still gives the same error

Ill add the class below

Player.h


Player.cpp



wasnt sure what parts would be valid to the question so the lot got attached >.> who knows might be of use to someone.

EDIT: To clarify ive used a various number of methods to try and create a player inside of an array.

Player ArrayName[5];
ArrayName[1] = MainPlayer;

or


Player PlayerList[20];

Player MainPlayer();
PlayerList[1] = MainPlayer;
PlayerList[1].InitPlayer(0, 0, 0, 0, 0);

Player SecondaryPlayer();
PlayerList[2] = SecondaryPlayer;
PlayerList[2].InitPlayer(1, 10, 0, 10, 1);


And a few others ;/ I just cant seem to get it to compile and work.

EDIT AGAIN: I should note that the error changed and i forgot to mention it >.> to many things going on at once

class Player __cdecl MainPlayer(void)" (?MainPlayer@@YA?AVPlayer@@XZ) referenced in function "void __cdecl DarkGDK(void)" (?DarkGDK@@YAXXZ)

is the error we are getting, we know MainPlayer isnt a real function, But to declare it normally without trying to save this to an array we can use

Player MainPlayer();

to create the Class object ;/

So im stuck and confused,

Stuck in the land of the confused! and loving it!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 22nd Mar 2008 02:30
I'm at a loss at this point.

The only thing different between what you're doing and how I handle it is that I actually put the definition of the constructor body in the class declaration file and leave it out of the .cpp file.



and ditto for the destructor. But that's just the way I learned it and it shouldn't have any different end effect.

I just ran your code through my compiler, though I had to leave out the collision stuff since I'm not set up to use it currently. I got no compiler errors whatsoever.

In theory the no default constructor message should be a warning, not a failure to compile. But the fact that you get the message in the presence of one is puzzling.

Lilith, Night Butterfly
Fusspawn
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 22nd Mar 2008 02:35
Edited the above post to show the error im having ( trying to take in to much at once. im busy working with Raknet and doing that and relaying messages back and forth >.> )

tho how it compiles for you and not us is beyond me, How did you create the player in the array if you dont mind posting that ?

Stuck in the land of the confused! and loving it!
Rye
21
Years of Service
User Offline
Joined: 30th May 2003
Location: United Kingdom, Blackrod
Posted: 22nd Mar 2008 02:43
when you use

Player PlayerList[5];

that actually MAKES 5 players
and each one has its contructor called.

all you need then is

PlayerList[0].InitPlayer(1, 10, 0, 10, 1);

You dont have to make another player and put it into the array.
Fusspawn
16
Years of Service
User Offline
Joined: 19th Feb 2008
Location:
Posted: 22nd Mar 2008 02:46
Ahhh....

Makes perfect sense now >.>

Thanks to you both for the help you offered and sorry for my bad attempt at passing on Messages >.>

Now thats outta the way we can crack on with the game

Stuck in the land of the confused! and loving it!
Rye
21
Years of Service
User Offline
Joined: 30th May 2003
Location: United Kingdom, Blackrod
Posted: 22nd Mar 2008 03:00
I think what you were trying to do was a mix of creation methods.

There is the one i said before and the one Lilith said

Player * PlayerList[5]; // this makes 5 pointers to players

PlayerList[0] = new Player(); // this makes a player, and makes PlayerList[0] POINT to the newly created object.

PlayerList[0]->InitPlayer(1, 10, 0, 10, 1); // how to call a function


There are advantages and disadvantages to each method.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 24th Mar 2008 18:49
Did you make empty constructor in the CPP file? I do if I need too..

header.h


code.cpp


Works for me... try it.

Login to post a reply

Server time is: 2024-09-29 15:17:18
Your offset time is: 2024-09-29 15:17:18