473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing an aray of objects question?

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
12 1584

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

"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
>
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

"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
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

"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
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
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

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

"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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bryan Martin | last post by:
I have a object that is created in a seperate domain which needs to be passed back to the parent class. Because this object is created in a seperate domain if I try to pass the object back to the...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
11
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to...
25
by: Stuart Hilditch | last post by:
Hi all, I am hoping that someone with some experience developing nTier apps can give me some advice here. I am writing an nTier web app that began with a Data Access Layer (DAL), Business...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
9
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
4
by: Deckarep | last post by:
Hello fellow C# programmers, This question is more about general practice and convention so here goes: I got into a discussion with a co-worker who insisted that as a general practice all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.