用宏定义是可以,我在程序里试了一下,不过好多人都说在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)
麻烦帮我看看,提示一下,谢谢啦!!!!