传递对象的问题? [英] Passing an aray of objects question?

查看:70
本文介绍了传递对象的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,我想将一个对象数组传递给

a函数,并且从那个对象我要返回一个valuse到那个

函数如何我是这样做的吗?


这就是我所拥有的:


terrain * t; < - 一系列不同种类的地形。


这是代码行:

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


这是构造函数:

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

b = bo; //板对象

t = te; //地形对象

xlen = x; //数组的大小

ylen = y;

if(xlen 99 || ylen 99){// map只能是100 x 100. br />
MessageBox(NULL,地图太大,编辑程序!,错误!,

MB_OK);

}

}

这是阵列:

静态地形trn [5]; //地形阵列


Terraine对象:

class terrain:public graphic,public color {// include my graphic lib

int mvcost; // movemtn cost to move throw那个空间

int defense; //实施时的防御调整


public:

terrain();

terra in(int,int,int);

void SetColor(int rn,int bn,int gn){r = rn; b = bn; g = gn;}

void SetAll(int,int,int,BYTE c []); //设置颜色和图形

void SetData(int,int); //设置防御加值和移动成本

int move()const {return mvcost;} //返回movemnt成本

int getDef()const {return defense;} < -the runcion我想访问。

};


回顾一下,我想将地形数组和地图对象发送到

地图管理器对象,并在该对象中将它们作为

对象的数据类型,然后当我需要时,我可以从地形访问数据

根据地图的空间键入。每个地形类型都是一个int,其中
对应于数组元素,该元素具有该地形的数据

类型。


ex 0 =打开,1 =水2 =树木等。

解决方案



faq描述问题周围阵列。
http://www.parashift .com / c ++ - faq-lite / containers.html


JoeC写道:


我写的一个程序,我想将一个对象数组传递给

a函数,从那个对象我要返回一个valuse到那个

函数我该怎么办? br />



它可以帮助您获得更小的代码片段,以更简洁的方式显示您想要做的事情。




" JoeC" < en ***** @ yahoo.comwrote in message

news:11 ********************* @ b28g2000cwb.googlegro ups.com ...


>我正在写一个程序,我想将一系列对象传递给

a函数和从那个对象我想返回一个valuse到那个

函数我该怎么做?



该函数被赋予一个指向对象的指针,对吗?然后简单地在该函数中编写代码

,使用 - >

运算符通过该指针访问对象。你有什么问题?


>

这就是我所拥有的:


地形* t; < - 一系列不同种类的地形。


这是代码行:

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



这不是有效的代码行。为什么会有一个(&after; after ; b->" ;?


那个代码在哪里?...在​​某个函数中知道什么是t,b,lp1和

lp2是吗?


>

这是构造函数:


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

b = bo; //板对象

t = te; //地形对象

xlen = x; //数组的大小

ylen = y;

if(xlen 99 || ylen 99){// map只能是100 x 100.

MessageBox(NULL,地图太大,编辑程序!,错误!,

MB_OK);

}

}


这是数组:

static terrain trn [5]; //地形数组


这个声明在哪里?这是某个班级的成员吗?某些函数中的本地

变量?如果它是一个全局变量,我不认为你是理解静态的含义。从这个意义上说。 (这与

不同,对于成员或本地变量使用静态。查找它,或谷歌

它,以获取更多信息。)


>

Terraine对象:

类地形:公共图形,公共颜色{//包括我的graphic lib

int mvcost; // movemtn cost to move throw that space

int defence; //实施防御性调整


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 []); //设置颜色和图形

void SetData(int,int); //设置防御加值和移动成本

int move()const {return mvcost;} //返回movemnt成本

int getDef()const {return defense;} < -the我要访问的runcion。



什么是runcion?


};


总结一下,我想将terrain数组和地图对象发送到

地图管理器对象,并在该对象中将它们作为

对象的数据类型然后当我需要的时候,我可以根据地图的空间从地形

类型访问数据。每个地形类型都是一个int,其中
对应于数组元素,该元素具有该地形的数据

类型。


ex 0 =打开,1 =水2 =树木打开。



好​​的,但是你问的问题是什么?你得到编译

错误?如果是这样,那是什么错误以及导致它的代码是什么?


请阅读常见问题解答(尤其是第5节,关于如何发布)。

-Howard



>

好​​的,但是你问的问题是什么?你得到编译

错误?如果是这样,那是什么错误以及导致它的代码是什么?


请阅读常见问题解答(尤其是第5节,关于如何发布)。


我的问题是它在这一行崩溃:

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

我想做的是将* t(asrray)和* b(对象)传递给另一个

对象,然后访问对象和数组中的信息

的一个功能。


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.

解决方案


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.



"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


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


这篇关于传递对象的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆