473,625 Members | 3,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1605

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******** *************@b 28g2000cwb.goog legroups.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,l p1)].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.goo glegroups.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,l p1)].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******** *************@h 48g2000cwc.goog legroups.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,l p1)].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,l p1)].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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1889
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 parent class (different domain) I receive an error about "File Not Found". I know the object is created successfully and the objects method can be called after loading the object however upon passing it back to the calling class it exploades. I...
25
2922
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 construction and then.. // some other processing and/or changing 'retval' return retval; }
13
17916
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 create another Area and assign myArea to it (ie: Area foo = myArea;) or is there a better way? ~AF
11
3177
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 the Data Access Layer method -or- 2) Pass some kind of a unique id to the Data Access Layer method
25
5045
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 Logic Layer (BLL) and User Interface Layer (UIL). The problem I found with this was circular referencing...
11
8114
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
12
5322
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 the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and the windows application. I face the following problem when I send this class to the webservice.
9
3781
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 techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing that object to the web service
4
2807
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 objects should be passed by reference using the ref keyword generally speaking because as the writer of code you are conveying your intentions that an Object should/can be modified by your function.
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1500
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.