启动一个类的二维数组 [英] initiating a 2d array of a class

查看:59
本文介绍了启动一个类的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯......不太确定怎么说这个。

i得到这个来编译..我不知道什么是正确的语法

这是...希望它是自我解释的。


这里是我的班级:

------

class TileMap

{

public:


int xw,yw;

int tw,th;

int rows,cols;

int fullrows,fullcols;

int minX,minY,maxX,maxY;


TileMapCell **网格;


TileMap(int rows,int cols,int xw,int yw):rows(rows),

cols(cols),xw(xw),yw(yw)

{

tw = xw * 2;

th = yw * 2;

fullrows = rows + 2;

fullcols = cols + 2;

minX = tw;

minY = th;

maxX = tw +(行* tw);

maxY = th +(行* th);

}


TileMapCell GetTile_S(int x,int y)

{

返回网格[int(x / tw) ] [int(y / th)];

}


TileMapCell GetTile_V(Vector2 v)

{

返回grid [int(vx / tw)] [int(vy / th)];

}


TileMapCell GetTile_I(int x,int y)

{

return grid [x] [y] ;

}


void GetIndex_S(Vector2 v,int x,int y)

{

vx = int(x / tw);

vy = int(y / th);

}


void GetIndex_V (Vector2 v,Vector2 p)

{

vx = int(px / tw);

vy = int(py / th);

}


void Building()

{

int x = xw;

int y = yw;

int fullrows = fullrows; //网格的ACTUAL大小,包括

边框瓷砖

int fullcols = fullcols;

int rows = rows;

int cols = cols;


//构建原始瓦片

for(int i = 0;我< fullrows; i ++)

{

grid [i] = new TileMapCell [fullcols]; //< ---需要帮助!!!

for(int j = 0; j< fullcols; j ++)

{

grid [i] [j] = new TileMapCell(i,j,x,y,xw,yw);

y + = th;

}

x + = tw;

y = yw;

}

}

};

--------------------

问题在这里:

- ----------------

//构建原始图块

for(int i = 0; i< fullrows; i ++ )

{

grid [i] = new TileMapCell [fullcols]; //< ---需要帮助!!!

for(int j = 0; j< fullcols; j ++)

{

grid [i] [j] = new TileMapCell(i,j,x,y,xw,yw);

y + = th;

}

x + = tw;

y = yw;

}

------------- ----

并且你想要问:

------------

class TileMapCell

{

public:


int ID;

int CTYPE;

int i,j; //网格中的图块索引

TileMapCell * nU,* nD,* nL,* nR; //邻近的瓷砖

int eU,eD,eL,eR; //边缘信息


float gx; //环境属性

浮动;

浮动d;


TileMapCell * next,* prev;


Vector2 pos;


int xw,yw;


float minx,maxx,miny,maxy;


int signx,signy,sx,sy;


TileMapCell(int i,int j,float x,float y,int xw,int yw ):我(i),

j(j),xw(xw),yw(yw)

{

pos.set(x ,y);


gx = 0;

gy = GRAV;

d = DRAG;

nU = nD = nL = nR = NULL;

eU = eD = eL = eR = EID_OFF;

next = prev = NULL;


minx = pos.x - xw;

maxx = pos.x + xw;

miny = pos.y - yw;

maxy = pos.y + yw;


signx = signy = sx = sy = 0;

}


无效LinkU(TileMapCell * t)

{

nU = t;

}

void LinkD(TileMapCell * t)

{

nD = t;

}

void LinkL (TileMapCell * t)

{

nL = t;

}

void LinkR(TileMapCell * t)

{

nR = t;

}

};

---- ---------------

和你告诉我之前


grid [i] = new TileMapCell [fullcols] ;


不起作用,因为没有TileMapCell()构造函数....我知道。

但它不应该在那时构建任何东西.. 。对吗?


如果您需要更多信息..只需留言。


感谢您的帮助。

uhhmmm... not really sure how to word this.
i cant get get this to compile.. i''m not sure what the proper syntax to
do this is.. hopefully it''s self explanatory.

here''s my class:
------
class TileMap
{
public:

int xw, yw;
int tw, th;
int rows, cols;
int fullrows, fullcols;
int minX, minY, maxX, maxY;

TileMapCell **grid;

TileMap(int rows, int cols, int xw, int yw) : rows(rows),
cols(cols), xw(xw), yw(yw)
{
tw = xw*2;
th = yw*2;
fullrows = rows+2;
fullcols = cols+2;
minX = tw;
minY = th;
maxX = tw + (rows*tw);
maxY = th + (rows*th);
}

TileMapCell GetTile_S(int x, int y)
{
return grid[int(x/tw)][int(y/th)];
}

TileMapCell GetTile_V(Vector2 v)
{
return grid[int(v.x/tw)][int(v.y/th)];
}

TileMapCell GetTile_I(int x, int y)
{
return grid[x][y];
}

void GetIndex_S(Vector2 v, int x, int y)
{
v.x = int(x/tw);
v.y = int(y/th);
}

void GetIndex_V(Vector2 v, Vector2 p)
{
v.x = int(p.x/tw);
v.y = int(p.y/th);
}

void Building()
{
int x = xw;
int y = yw;
int fullrows = fullrows;//the ACTUAL size of the grid, including
border tiles
int fullcols = fullcols;
int rows = rows;
int cols = cols;

//build raw tiles
for(int i = 0; i < fullrows; i++)
{
grid[i] = new TileMapCell[fullcols]; // <--- need help here!!!
for(int j = 0; j < fullcols; j++)
{
grid[i][j] = new TileMapCell(i,j,x,y,xw,yw);
y += th;
}
x += tw;
y = yw;
}
}
};
--------------------
the problem is in here:
------------------
//build raw tiles
for(int i = 0; i < fullrows; i++)
{
grid[i] = new TileMapCell[fullcols]; // <--- need help here!!!
for(int j = 0; j < fullcols; j++)
{
grid[i][j] = new TileMapCell(i,j,x,y,xw,yw);
y += th;
}
x += tw;
y = yw;
}
-----------------
and incase you were about to ask:
------------
class TileMapCell
{
public:

int ID;
int CTYPE;
int i,j; // index of tile in grid

TileMapCell *nU, *nD, *nL, *nR; // neighboring tiles
int eU, eD, eL, eR; // edge info

float gx; // environmental properties
float gy;
float d;

TileMapCell *next, *prev;

Vector2 pos;

int xw, yw;

float minx, maxx, miny, maxy;

int signx, signy, sx, sy;

TileMapCell(int i, int j, float x, float y, int xw, int yw) : i(i),
j(j), xw(xw), yw(yw)
{
pos.set(x,y);

gx = 0;
gy = GRAV;
d = DRAG;

nU = nD = nL = nR = NULL;
eU = eD = eL = eR = EID_OFF;
next = prev = NULL;

minx = pos.x - xw;
maxx = pos.x + xw;
miny = pos.y - yw;
maxy = pos.y + yw;

signx = signy = sx = sy = 0;
}

void LinkU(TileMapCell *t)
{
nU = t;
}
void LinkD(TileMapCell *t)
{
nD = t;
}
void LinkL(TileMapCell *t)
{
nL = t;
}
void LinkR(TileMapCell *t)
{
nR = t;
}
};
-------------------
and before you tell me

grid[i] = new TileMapCell[fullcols];

doesnt work because there is no TileMapCell() constructor.... I know.
but it shouldnt really be constructing anything at that point... right?

if you need more info.. just leave a message.

thanks for the help.

推荐答案

Mark写道:

....
Mark wrote:
....
在你告诉我之前

grid [i] = new TileMapCell [fullcols];

不起作用,因为没有TileMapCell()构造函数....我知道。
但它不应该真的是在这一点上构建任何东西......对吗?

如果您需要更多信息..只需留言。
and before you tell me

grid[i] = new TileMapCell[fullcols];

doesnt work because there is no TileMapCell() constructor.... I know.
but it shouldnt really be constructing anything at that point... right?

if you need more info.. just leave a message.




看来你想要创建一个指针数组


TileMapCell.grid [i] =新的TileMapCell * [fullcols];



It appears you want to create an array of pointers to

TileMapCell.grid[i] = new TileMapCell*[fullcols];


hmm ..?


感谢快速回复...但这没有任何意义......


基本上如果#代表一个单元格,如果我的理解是正确的,那么

grid [i]代表










....


我们想在x维度添加一些单元格...


###

###

###

## #

....


但是你说的是我们在2D /或其他(* [])中添加2D内容每行
??

反正..我试过了,并且


grid [i] = new TileMapCell * [fullcols]; //< ---需要帮助!!!


也没编译。

说:

无法转换` TileMapCell **''到'TileMapCell *''在任务中


所以就像我说..它试图把2维的东西变成

1 ...

hmm..?

thanks for the fast reply... but that doesn''t make any sense...

basically if # represents a cell, if my understanding is correct, then
grid[i] would represent

#
#
#
#
....

and we want to add some cells to the x dimension...

###
###
###
###
....

but what you''re saying is we add a 2D something or other ( *[] ) to
each row??
anyways.. i tried that, and

grid[i] = new TileMapCell*[fullcols]; // <--- need help here!!!

doesnt compile either.
says:
cannot convert `TileMapCell**'' to `TileMapCell*'' in assignment

so like i said.. its trying to turn something with 2 dimensions into
something with 1...


*标记:
嗯...不太确定怎么说这个。
我不能得到这个编译..我不知道
这样做的正确语法是什么..希望它是自我解释的。


解决方案:使用标准库类,并使用Boost库,

_don''t_做原始数组或原始指针。

这里是我的班级:
------
类TileMap
{
公开:

int xw,yw;
int tw,th;
int rows,cols;
int fullrows,fullcols;
int minX,minY,maxX,maxY;


当然你不希望上述成员公开。将它们设为私有。


TileMapCell ** grid;
uhhmmm... not really sure how to word this.
i cant get get this to compile.. i''m not sure what the proper syntax to
do this is.. hopefully it''s self explanatory.
Solution: use standard library classes, and use the Boost library, and
_don''t_ do raw arrays or raw pointers.
here''s my class:
------
class TileMap
{
public:

int xw, yw;
int tw, th;
int rows, cols;
int fullrows, fullcols;
int minX, minY, maxX, maxY;
Surely you don''t want the above members public. Make them private.

TileMapCell **grid;




转到< url:http://www.boost .org / libs / multi_array / doc / user.html> ;.

Hth。,


- Alf

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



Go to <url: http://www.boost.org/libs/multi_array/doc/user.html>.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于启动一个类的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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