help with object array

I am trying to create random objects, then i was to go through and count how many of each type I have, but im not quite sure how to handle object arrays. Here is my code

#include <stdio.h>
#include <irrlicht.h>

#include <time.h>

int lobbies;
int i;


class server
{
int occupants;

int speed;

public:
void setOccupants(int number)
{
occupants = number;
}

int getOccupants()
{
return occupants;
}

void setSpeed(int number)
{
speed = number;
}

int getSpeed(void)
{
return speed;
}

};

server createServers(void)
{
server objects[50];

int randomSpeed;
int randomOccupants;

//initialize random seed
srand(time(NULL));

lobbies = rand()% 5 + 25;
for(i=0; i < lobbies; i++)
{
randomSpeed = rand()% 3 + 1;
objects[i].setSpeed(randomSpeed);

randomOccupants = rand() % 50;
objects[i].setOccupants(randomOccupants);
}
return objects;
}


int numberOfLobbies(int selectedTime, server object)
{
int speedVar;
int x = 0;

if(selectedTime == 0)
{
for(i=0; i < lobbies; i++)
{
speedVar = object[i].getSpeed();
if(speedVar == 0)
{
x++;
}

}
return x;

}

if(selectedTime == 1)
{
for(i=0; i < lobbies; i++)
{
speedVar = object[i].getSpeed();
if(speedVar == 1)
{
x++;
}

}
return x;

}

if(selectedTime == 2)
{
for(i=0; i < lobbies; i++)
{
speedVar = object[i].getSpeed();
if(speedVar == 2)
{
x++;
}

}
return x;

}

}

The problem is something to do with the brackets for the objects or something
Returning a single server object is not the same as returning 50 server objects. Your createServers() function is not correct.

I have never returned an array from a C function before, so I'm unsure about the syntax, so I'll just give you a different function prototype (which works far better than returning the array because returning the array forces duplication of 50 server objects while the following doesn't):

void createServers(server object[]);

And you call it like this:

1
2
server myServers[50];
createServers(myServers);


And please use code tags when posting code: [code]//Code goes here. [/code]

EDIT: One more thing: Since arrays don't know their own size, the function createServers() (and any function that receives an array) should also receive the maximum number of elements the array holds. So correcting myself:


void createServers(server object[], int cElements);

And you call it like this:

1
2
server myServers[50];
createServers(myServers, _countof(myServers));


If your compiler doesn't know what "_countof" is, define it like this:

1
2
3
#ifndef _countof
#define _countof(X) (sizeof(X) / sizeof(X[0]))
#endif 


Finally, make sure the function createServers() never attempts to read or write beyond the array's capacity. In your case, make sure that the variable 'i' in the FOR loop never equals or exceeds cElements.
Last edited on
what is the point of cElements? do i use it as in
return objects[cElements];
No, you don't return anything from createServers(). The point of cElements is to make sure createServers() doesn't step outside of the array you pass to it.
I'm not quite sure what its supposed to do if i dont use it besides in the parameter, but i got it to work using it the way i asked. Thanks
You use it to keep track of the server array's size.

1
2
3
server servers[50];

servers[55] ...; //this would cause a segfault because servers only has 50 elements 


The cElements int is meant to prevent the above issue from happening.
Topic archived. No new replies allowed.