Passing an aray of objects question?

J

JoeC

I am writing a program and I would like to pass an array of objects to
a function and from that object I want to return a valuse to that
function how do I do it?

Here is what I have:

terrain * t; <- an array of different kinds of terrain.

This is the line of code:
int mod = t[b->(GetSpace(lp2,lp1)]->getDef();

Here is the constructor:

mapmgt::mapmgt(board * bo, terrain * te, int x, int y){
b = bo; //board object
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}


Here is the array:
static terrain trn[5]; //terrain array

Terraine object:
class terrain : public graphic, public color{ //inclueds my graphic lib
int mvcost; //movemtn cost to move throw that space
int defence; //defensive adjustment when implemented

public:
terrain();
terrain(int, int, int);
void SetColor(int rn, int bn, int gn){r = rn; b = bn; g = gn;}
void SetAll(int, int, int, BYTE c[]); //sets the color and graphic
void SetData(int, int); //sets defensive bonus and movment cost
int move()const {return mvcost;} //return the movemnt cost
int getDef()const {return defence;} <-the runcion I want to access.
};

To recap, I want to send the terrain array and the map object to the
map manager object and in that object have them as data types of the
object then when I need then I can access the data from the terrain
type based on a space of the map. Each terrain type is an int which
coresponds to the array element which has the data for that terrain
type.

ex 0 = open, 1 = water 2 = trees and on.
 
H

Howard

JoeC said:
I am writing a program and I would like to pass an array of objects to
a function and from that object I want to return a valuse to that
function how do I do it?

The function is given a pointer to an object, right? Then simply write code
in that function which accesses the object via that pointer, using the ->
operator. What problem are you having?
Here is what I have:

terrain * t; <- an array of different kinds of terrain.

This is the line of code:
int mod = t[b->(GetSpace(lp2,lp1)]->getDef();

That's not a valid line of code. Why is there a "(" after "b->"?

And where is that code?...in some function which knows what t, b, lp1 and
lp2 are?
Here is the constructor:

mapmgt::mapmgt(board * bo, terrain * te, int x, int y){
b = bo; //board object
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}


Here is the array:
static terrain trn[5]; //terrain array

Where is this declaration? Is this a member of some class? A local
variable in some function? If it's a global variable, I don't think you're
understanding the meaning of "static" in that sense. (It's different from
the use of "static" for a member or local variable. Look it up, or google
it, for more info.)
Terraine object:
class terrain : public graphic, public color{ //inclueds my graphic lib
int mvcost; //movemtn cost to move throw that space
int defence; //defensive adjustment when implemented

public:
terrain();
terrain(int, int, int);
void SetColor(int rn, int bn, int gn){r = rn; b = bn; g = gn;}
void SetAll(int, int, int, BYTE c[]); //sets the color and graphic
void SetData(int, int); //sets defensive bonus and movment cost
int move()const {return mvcost;} //return the movemnt cost
int getDef()const {return defence;} <-the runcion I want to access.

What's a runcion?
};

To recap, I want to send the terrain array and the map object to the
map manager object and in that object have them as data types of the
object then when I need then I can access the data from the terrain
type based on a space of the map. Each terrain type is an int which
coresponds to the array element which has the data for that terrain
type.

ex 0 = open, 1 = water 2 = trees and on.

Ok, but what's the problem you're asking about? Are you getting a compile
error? If so, what's the error and what code is causing it?

Please read the FAQ (especially section 5, on how to post).

-Howard
 
J

JoeC

Ok, but what's the problem you're asking about? Are you getting a compile
error? If so, what's the error and what code is causing it?

Please read the FAQ (especially section 5, on how to post).
My problem is that it crashes at this line:
int mod = t[b->GetSpace(lp2,lp1)].getDef();
What I want to do is pass *t (asrray) and *b(object) to another
object and then access the information in the object and array inside
of a function.
 
H

Howard

JoeC said:
Ok, but what's the problem you're asking about? Are you getting a
compile
error? If so, what's the error and what code is causing it?

Please read the FAQ (especially section 5, on how to post).
My problem is that it crashes at this line:
int mod = t[b->GetSpace(lp2,lp1)].getDef();
What I want to do is pass *t (asrray) and *b(object) to another
object and then access the information in the object and array inside
of a function.

Where in the above code are you passing *t and *b to a function? What
function do you want to pass them to? And are you sure you want to pass *t
and *b, and not t and b? How is the function declared you're talking about?

Or...is this line perhaps _inside_ the function you're talking about? If
so, then you need to check that the values you're passing in for t and b are
valid. Either you need to review your code to see whether those pointers
are always valid, or you need to add code which ensures that they're valid
before using them.

(In any case, you're still not posting enough information for anyone to be
able to help you.)

-Howard
 
J

JoeC

Where in the above code are you passing *t and *b to a function? What
function do you want to pass them to? And are you sure you want to pass *t
and *b, and not t and b? How is the function declared you're talking about?

Or...is this line perhaps _inside_ the function you're talking about? If
so, then you need to check that the values you're passing in for t and b are
valid. Either you need to review your code to see whether those pointers
are always valid, or you need to add code which ensures that they're valid
before using them.

(In any case, you're still not posting enough information for anyone to be
able to help you.)

class mapmgt{
int xlen; //map size (max 100)
int ylen;

table tbl; //combat results table
hold h[100][100]; //allows for a map max 100x100.
board * b; //holds board
terrain * t; //hold terrain

mapmgt::mapmgt(board * bo, terrain * te, int x, int y){
b = bo; //board object
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}
 
H

Howard

JoeC said:
Where in the above code are you passing *t and *b to a function? What
function do you want to pass them to? And are you sure you want to pass
*t
and *b, and not t and b? How is the function declared you're talking
about?

Or...is this line perhaps _inside_ the function you're talking about? If
so, then you need to check that the values you're passing in for t and b
are
valid. Either you need to review your code to see whether those pointers
are always valid, or you need to add code which ensures that they're
valid
before using them.

(In any case, you're still not posting enough information for anyone to
be
able to help you.)

class mapmgt{
int xlen; //map size (max 100)
int ylen;

table tbl; //combat results table
hold h[100][100]; //allows for a map max 100x100.
board * b; //holds board
terrain * t; //hold terrain

mapmgt::mapmgt(board * bo, terrain * te, int x, int y){
b = bo; //board object
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}

You've already posted that constructor previously. It was of no help the
first time, and it's still of no help.

Are you being deliberately obtuse? You completely ignore most of my
questions, post snippets of code that don't seem to have anything to do with
the problem, and ask open-ended questions without posting the specific code
that's causing the problem.

If you can't help us help you, then I'm done trying.
 
J

JoeC

You've already posted that constructor previously. It was of no help the
first time, and it's still of no help.

What elese do I need to post? If it is wrong how do I do it right? I
sent the poitners to the objects store them in other pointers then use
them.
Are you being deliberately obtuse? You completely ignore most of my
questions, post snippets of code that don't seem to have anything to do with
the problem, and ask open-ended questions without posting the specific code
that's causing the problem.

If you can't help us help you, then I'm done trying.

Honestly, I have not been putting too much effort in this part of my
program, I have been focused on something else. I believe that I need
to do some research on the topic. Here is the whole block of code I
want to work with:

void mapmgt::fight(){
float atk = 0;
float def = 0;
float odds = 0;
for(int lp1 = 0; lp1 < ylen; lp1++){
for(int lp2 = 0; lp2 < xlen; lp2++){
if(h[lp2][lp1].at.size() > 0 && h[lp2][lp1].dt.size() > 0){
atk = h[lp2][lp1].getAt();
def = h[lp2][lp1].getDt();
odds = atk/def;
int roll = (rand()%6)+1;
//int mod = t[b->GetSpace(lp2,lp1)].getDef();
//roll+=mod;
int res = tbl.result(roll, odds);
if(res == 0){h[lp2][lp1].killA();} //atk elim
if(res == 1){h[lp2][lp1].dispA();} //atk disp
if(res == 2){h[lp2][lp1].dispD();} //def disp
if(res == 3){h[lp2][lp1].killD();} //def elim
}
}
}
}

The problem that I am having is the statments that are commented out.
My map (b) is an object and my terrain(t) is an array of objects. The
map holds the types of terrain (ints) and the terrain will return the
actual modifer of that space.

I was just going to send pointers to the map and the terrain array to
this map manager opbect *t, *b, and just use them like values.
 
J

JoeC

You've already posted that constructor previously. It was of no help the
first time, and it's still of no help.

Are you being deliberately obtuse? You completely ignore most of my
questions, post snippets of code that don't seem to have anything to do with
the problem, and ask open-ended questions without posting the specific code
that's causing the problem.

If you can't help us help you, then I'm done trying.

What am I doing wrong, this is all I have, if it is completely wrong
let me know. I have notheing else revelent in my program.

mapmgt::mapmgt(board * bo, terrain * te, int x, int y){
b = bo; //board object
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}

class mapmgt{
int xlen; //map size (max 100)
int ylen;

table tbl; //combat results table
hold h[100][100]; //allows for a map max 100x100.
board * b; //holds board
terrain * t; //hold terrain

int mod = t[b->GetSpace(lp2,lp1)].getDef();

The function works outside of this object so there is no other problem.
The problem I am having is passing the the data to the object then
using it later.
 
P

peter koch

JoeC said:
I am writing a program and I would like to pass an array of objects to
a function and from that object I want to return a valuse to that
function how do I do it?

Here is what I have:

terrain * t; <- an array of different kinds of terrain.

t is NOT an array. It is a pointer.
This is the line of code:
int mod = t[b->(GetSpace(lp2,lp1)]->getDef();

Here is the constructor:

mapmgt::mapmgt(board * bo, terrain * te, int x, int y){

This constructor is passed two pointers and two ints. There is no array
in sight.
b = bo; //board object
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}


Here is the array:
static terrain trn[5]; //terrain array
[snip]

The question you ask tells me you are not yet in a position to program
with pointers. Forget it for now and start reading about std::vector.
Then replace your C-arrays with vectors and pointers with references
whereever you can. If you run into problem, we will be happy to help.
When you've done that and learned how to use the standard library, you
will be in a far better position to learn when and how to use pointers.

/Peter
 
H

Howard

JoeC said:
You've already posted that constructor previously. It was of no help the
first time, and it's still of no help.

What elese do I need to post? If it is wrong how do I do it right? I
sent the poitners to the objects store them in other pointers then use
them.
Are you being deliberately obtuse? You completely ignore most of my
questions, post snippets of code that don't seem to have anything to do
with
the problem, and ask open-ended questions without posting the specific
code
that's causing the problem.

If you can't help us help you, then I'm done trying.

Honestly, I have not been putting too much effort in this part of my
program, I have been focused on something else. I believe that I need
to do some research on the topic. Here is the whole block of code I
want to work with:

void mapmgt::fight(){
float atk = 0;
float def = 0;
float odds = 0;
for(int lp1 = 0; lp1 < ylen; lp1++){
for(int lp2 = 0; lp2 < xlen; lp2++){
if(h[lp2][lp1].at.size() > 0 && h[lp2][lp1].dt.size() > 0){
atk = h[lp2][lp1].getAt();
def = h[lp2][lp1].getDt();
odds = atk/def;
int roll = (rand()%6)+1;
//int mod = t[b->GetSpace(lp2,lp1)].getDef();

Use your debugger. That's what it's for.

Check the value of b. Is it valid?

Check what happens inside GetSpace. Are you getting a valid int result?

Assign the results of b->GetSpace(lp2,lp1) to an int. Then check if that
int is in the range of objects stored in t.
//roll+=mod;
int res = tbl.result(roll, odds);
if(res == 0){h[lp2][lp1].killA();} //atk elim
if(res == 1){h[lp2][lp1].dispA();} //atk disp
if(res == 2){h[lp2][lp1].dispD();} //def disp
if(res == 3){h[lp2][lp1].killD();} //def elim
}
}
}
}

The problem that I am having is the statments that are commented out.
My map (b) is an object and my terrain(t) is an array of objects. The
map holds the types of terrain (ints) and the terrain will return the
actual modifer of that space.

I was just going to send pointers to the map and the terrain array to
this map manager opbect *t, *b, and just use them like values.

What do you mean by *t and *b? You've said that t is an array. If it is,
then *t doesn't make any sense. (I've asked this before, and you ignored
me. I'm done now.)
 
J

JoeC

peter said:
JoeC said:
I am writing a program and I would like to pass an array of objects to
a function and from that object I want to return a valuse to that
function how do I do it?

Here is what I have:

terrain * t; <- an array of different kinds of terrain.

t is NOT an array. It is a pointer.
This is the line of code:
int mod = t[b->(GetSpace(lp2,lp1)]->getDef();

Here is the constructor:

mapmgt::mapmgt(board * bo, terrain * te, int x, int y){

This constructor is passed two pointers and two ints. There is no array
in sight.
b = bo; //board object
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}


Here is the array:
static terrain trn[5]; //terrain array
[snip]

The question you ask tells me you are not yet in a position to program
with pointers. Forget it for now and start reading about std::vector.
Then replace your C-arrays with vectors and pointers with references
whereever you can. If you run into problem, we will be happy to help.
When you've done that and learned how to use the standard library, you
will be in a far better position to learn when and how to use pointers.

Possubly, I fixed the problem by:

mapmgt::mapmgt(terrain * te, int x, int y){
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
b = new board;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}

class mapmgt{
int xlen; //map size (max 100)
int ylen;

table tbl; //combat results table
hold h[100][100]; //allows for a map max 100x100.
terrain * t; //hold terrain
board * b;

I just re-created the board in the object, it now seems to work fine.
I was doing some experiments with pointers in a simple program. I may
have to do some work to use pointers to objects in functions and
objects.
 
J

JoeC

peter said:
JoeC said:
I am writing a program and I would like to pass an array of objects to
a function and from that object I want to return a valuse to that
function how do I do it?

Here is what I have:

terrain * t; <- an array of different kinds of terrain.

t is NOT an array. It is a pointer.
This is the line of code:
int mod = t[b->(GetSpace(lp2,lp1)]->getDef();

Here is the constructor:

mapmgt::mapmgt(board * bo, terrain * te, int x, int y){

This constructor is passed two pointers and two ints. There is no array
in sight.
b = bo; //board object
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}


Here is the array:
static terrain trn[5]; //terrain array
[snip]

The question you ask tells me you are not yet in a position to program
with pointers. Forget it for now and start reading about std::vector.
Then replace your C-arrays with vectors and pointers with references
whereever you can. If you run into problem, we will be happy to help.
When you've done that and learned how to use the standard library, you
will be in a far better position to learn when and how to use pointers.
I think I solved the problem by just re-creating the object inside the
object:

class mapmgt{
int xlen; //map size (max 100)
int ylen;

table tbl; //combat results table
hold h[100][100]; //allows for a map max 100x100.
terrain * t; //hold terrain
board * b;

mapmgt::mapmgt(terrain * te, int x, int y){
t = te; //terrain object
xlen = x; //size of the array
ylen = y;
b = new board;
if(xlen > 99 || ylen > 99){ // map can only be 100 x 100.
MessageBox(NULL, "The map is too big, edit the program!", "Error!",
MB_OK);
}
}

You can and now my program works pretty well. You can see the whole
program
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=10766&lngWId=3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top