| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1706 人关注过本帖
标题:[求助]动态数组的宏定义
只看楼主 加入收藏
梅子
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-5-9
收藏
 问题点数:0 回复次数:7 
[求助]动态数组的宏定义
请教:
我从网上看到有关二维动态数组的宏定义的,当定义的数组多时,就可以用宏定义吧。
#define matrix_allocate(matrix,width,height,TYPE)
{
  matrix = new TYPE *[height];
  for(int i = 0; i < height; i++)
  matrix[i] = new TYPE[width];
}

#define matrix_delete(matrix,width,height)
{
  for(int i = 0; i < height; i++)
  delete [] matrix[i];
  delete [] matrix;
  matrix = 0;
}
这样表示的,但是为什么一运行时,就出这种提示呢:
error C2447: missing function header (old-style formal list?)
是怎么回事啊?这个宏定义的有问题吗?
搜索更多相关主题的帖子: 定义 动态 
2006-05-09 19:11
梅子
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-5-9
收藏
得分:0 
我知道怎么回事了呵呵,应该在每一行换行时加换行符\,这样就可以了。

2006-05-10 16:41
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 

正确的应该是这样写吧:
#define matrix_allocate(matrix,width,height,TYPE) void matrix_allocate(matrix,width,height,TYPE) \
{\
  matrix = new TYPE *[height];\
  for(int i = 0; i < height; i++)\
  matrix[i] = new TYPE[width];\
}

#define matrix_delete(matrix,width,height) void matrix_delete(matrix,width,height)\
{\
  for(int i = 0; i < height; i++)\
  delete [] matrix[i];\
  delete [] matrix;\
  matrix = 0;\
}

但是在主函数里,你怎么给TYPE传递类型信息呢?,你能实现吗?
我试了一下,好困难啊
不过,我感觉用函数摸板就很容易实现了。

[此贴子已经被作者于2006-5-10 17:51:15编辑过]


woyaochengshuyidianle 我真的什么也不会
2006-05-10 17:40
梅子
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-5-9
收藏
得分:0 

用宏定义是可以,我在程序里试了一下,不过好多人都说在C++里,应该用inline,我还没细看.至于楼上说的函数模板我看看有什么不懂的再向你请教啊.
现在还有另外一个问题,请高手指点.
程序是关于指针做实参,数组做形参的,这一部分就是搞不明白。如下:

double sum(double sum[N][M])
{
double result=0.0;
int k,l;

for(k=0;k<N;k++)
for(l=0;l<M;l++)
{
result+=sum[k][l]>0?sum[k][l]:(-sum[k][l]);
}

return result;
}

double subsum(double sub1[N][M],double sub2[N][M])
{
double subresult=0.0;
int m,n;

for(m=0;m<N;m++)
for(n=0;n<M;n++)
{
subresult+=(sub1[m][n]-sub2[m][n])>0?(sub1[m][n]-sub2[m][n]):(sub2[m][n]-sub1[m][n]);
}

return subresult;
}

void main()
{
double **us = 0;
M2allo(us,N,M,double);\\根据宏定义动态二维数组,宏定义我没往上粘,就是把us、vs等定义成二维动态数组

double **vs = 0;
M2allo(vs,N,M,double);

double **vx = 0;
M2allo(vx,N,M,double);

double **vy = 0;
M2allo(vy,N,M,double);

......
error = subsum(us,vx)/sum(us);
error1 = subsum(vs,vy)/sum(vs);
error = error +error1;
....
}

提示的错误如下:

正确程序(动态数组).cpp
F:\我的文档\program\lattice boltzmann code\正确程序(动态数组).cpp(493) : error C2664: 'subsum' : cannot convert parameter 1 from 'double ** ' to 'double [][51]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\我的文档\program\lattice boltzmann code\正确程序(动态数组).cpp(493) : error C2664: 'sum' : cannot convert parameter 1 from 'double ** ' to 'double [][51]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\我的文档\program\lattice boltzmann code\正确程序(动态数组).cpp(494) : error C2664: 'subsum' : cannot convert parameter 1 from 'double ** ' to 'double [][51]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\我的文档\program\lattice boltzmann code\正确程序(动态数组).cpp(494) : error C2664: 'sum' : cannot convert parameter 1 from 'double ** ' to 'double [][51]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

正确程序(动态数组).obj - 4 error(s), 0 warning(s)

麻烦帮我看看,提示一下,谢谢啦!!!!


2006-05-11 08:30
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

可以直接为2级指针分配内存啊?我也想知道怎么弄,我以前弄过,总是放弃,然后用1级指针数组代替


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-05-11 10:00
梅子
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-5-9
收藏
得分:0 
动态数组不是就是指针吗,我对这一部分内容理解的很差,希望有高手能指点一下!

2006-05-11 16:30
静思
Rank: 3Rank: 3
来 自:沈阳
等 级:新手上路
威 望:8
帖 子:630
专家分:0
注 册:2006-2-28
收藏
得分:0 
我也是,希望哪位高手指点一下,这部分没怎么学好

英者自知,雄者自胜
2006-05-14 00:34
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
好象2维数组只能用一级指针对应,或则用1级指针数组,我是这么认为的

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-05-14 01:25
快速回复:[求助]动态数组的宏定义
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018401 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved