By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

Passing an aray of objects question?

P: n/a
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.

Sep 18 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a

The faq describes issues surrounding arrays.
http://www.parashift.com/c++-faq-lite/containers.html

JoeC wrote:
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?
It would help you you could have a smaller snippet of code that shows
what you're trying to do in a more concise fashion.
Sep 18 '06 #2

P: n/a

"JoeC" <en*****@yahoo.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
>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

Sep 18 '06 #3

P: n/a
>
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.

Sep 19 '06 #4

P: n/a

"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>
>>
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


Sep 19 '06 #5

P: n/a
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);
}
}

Sep 20 '06 #6

P: n/a

"JoeC" <en*****@yahoo.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>
>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.

Sep 21 '06 #7

P: n/a
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.

Sep 21 '06 #8

P: n/a
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.

Sep 21 '06 #9

P: n/a

JoeC wrote:
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

Sep 21 '06 #10

P: n/a

"JoeC" <en*****@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>
>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.)

Sep 21 '06 #11

P: n/a

peter koch wrote:
JoeC wrote:
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.

Sep 21 '06 #12

P: n/a

peter koch wrote:
JoeC wrote:
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/s...10766&lngWId=3

Sep 21 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.