我自己写了小程序,但在VC6.0下编译不过。
//test2d.htemplate <int fHeight,int fWidth>
void print2d(float (&filter)[fHeight][fWidth])
{ for(int i = 0; i < fHeight ; i++)
{
for(int j = 0; j < fWidth; j++)
{
printf("filter[ %d ][ %d ] = %f \n",i,j,filter[i][j]);
}
}
}//test2d.cpp#include <stdio.h>
#include "test2d.h"
int main()
{
float filter[3][3]={ { 0.0 , 0.25 , 0.0 },
{ 0.25, 0.0 , 0.25},
{ 1.0 , 0.25 , 0.0 }};
print2d(filter);
return 0;
}
//test2d.htemplate <int fHeight,int fWidth>
void print2d(float (&filter)[fHeight][fWidth])
{ for(int i = 0; i < fHeight ; i++)
{
for(int j = 0; j < fWidth; j++)
{
printf("filter[ %d ][ %d ] = %f \n",i,j,filter[i][j]);
}
}
}//test2d.cpp#include <stdio.h>
#include "test2d.h"
int main()
{
float filter[3][3]={ { 0.0 , 0.25 , 0.0 },
{ 0.25, 0.0 , 0.25},
{ 1.0 , 0.25 , 0.0 }};
print2d(filter);
return 0;
}
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.test2d.cpp
test2d.h(4) : error C2265: '<Unknown>' : reference to a zero-sized array is ille
gal
test2d.h(4) : error C2087: '<Unknown>' : missing subscript
test2d.cpp(11) : error C2783: 'void __cdecl print2d(float (&)[1][1])' : could no
t deduce template argument for 'fHeight'
test2d.cpp(11) : error C2783: 'void __cdecl print2d(float (&)[1][1])' : could no
t deduce template argument for 'fWidth'
void print2d(float *filter,int fHeight,int fWidth])
{ for(int i = 0; i < fHeight ; i++)
{
for(int j = 0; j < fWidth; j++)
{
printf("filter[ %d ][ %d ] = %f \n",i,j,filter[i][j]);
}
}
}//test2d.cpp#include <stdio.h>
#include "test2d.h"
int main()
{
float filter[3][3]={ { 0.0 , 0.25 , 0.0 },
{ 0.25, 0.0 , 0.25},
{ 1.0 , 0.25 , 0.0 }};
print2d(filter,3,3);
return 0;
}
原来还是要强制类型转换的,原来vc对二维数组是连续存放的。#include <stdio.h>
//#include "test2d.h"
void print2d(float *filter,int fHeight,int fWidth)
{ for(int i = 0; i < fHeight ; i++)
{
for(int j = 0; j < fWidth; j++)
{
printf("filter[ %d ][ %d ] = %f \n",i,j,*(filter+i*fWidth + j));
}
}
}
int main()
{
float filter[3][3]={ { 0.0 , 0.25 , 0.0 },
{ 0.25, 0.0 , 0.25},
{ 1.0 , 0.25 , 0.0 }};
print2d((float *)filter,3,3);
return 0;
}