error C2109: subscript requires array or pointer type 怎么解决;?

duyouxuanduyouxuan 2009-05-27 09:33:51
// 写入xml文件相关编码器和解码器关联信息 unicode
bool CDlgSwitch::OnWriteConnEnDeFile(wchar_t* xmlFile , wchar_t* elementArray)
{
if(elementArray == NULL)
return FALSE;
CMarkup* connect = new CMarkup();
size_t nodeNumber = sizeof(elementArray)/sizeof(elementArray[0]);
for(int i = 0; i < nodeNumber; i++)
{

LPCWSTR enIP = (LPCWSTR)(&(elementArray[i][1][0]));
CString enPort = (LPCWSTR)elementArray[i][2][0];
CString enChannel = (LPCWSTR)elementArray[i][3][0];
CString deIP = (LPCWSTR)elementArray[i][4][0];
CString dePort = (LPCWSTR)elementArray[i][5][0];
CString deChannel = (LPCWSTR)elementArray[i][6][0];
CString time = (LPCWSTR)elementArray[i][7][0];

//写入xml文件
connect->AddChildElem(LPCTSTR(_T("MAP")) );
connect->IntoElem();
connect->AddChildElem( LPCTSTR(_T("TYPE")), (LPCTSTR) type);
connect->AddChildElem( LPCTSTR(_T("ENIP")), (LPCTSTR) enIP);
connect->AddChildElem( LPCTSTR(_T("ENPORT")), (LPCTSTR) enPort);
connect->AddChildElem( LPCTSTR(_T("ENCHAN")), (LPCTSTR) enChannel);
connect->AddChildElem( LPCTSTR(_T("EDIP")), (LPCTSTR) deIP);
connect->AddChildElem( LPCTSTR(_T("DEPORT")), (LPCTSTR) dePort);
connect->AddChildElem( LPCTSTR(_T("DECHAN")), (LPCTSTR) deChannel);
connect->AddChildElem( LPCTSTR(_T("TIME")), (LPCTSTR) time);
connect->OutOfElem();
connect->save((LPCTSTR)xmlFile);
}
delete connect;
return TRUE;
}


g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(483) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(484) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(485) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(486) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(487) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(488) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(489) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(494) : error C2065: 'type' : undeclared identifier
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(529) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(535) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(542) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(548) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(554) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(560) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(566) : error C2109: subscript requires array or pointer type
g:\project\复件 theresultect (2)\coderconfigtool\dlgswitch.cpp(572) : error C2109: subscript requires array or pointer type
...全文
7425 31 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
ma328762444 2009-11-23
  • 打赏
  • 举报
回复
帮顶!
ma328762444 2009-11-23
  • 打赏
  • 举报
回复
建议楼主把错误的那几行删掉,重新敲一遍,如果仍然错误,再参看楼上各位的意见。
aaaa3105563 2009-05-28
  • 打赏
  • 举报
回复
帮顶
duyouxuanduyouxuan 2009-05-27
  • 打赏
  • 举报
回复
* elementArray = new wchar_t[8];
好像有问题?
duyouxuanduyouxuan 2009-05-27
  • 打赏
  • 举报
回复
wchar_t (** elementArray)[31]; 怎么样呢??
你看如何。
goodname 2009-05-27
  • 打赏
  • 举报
回复
天晚了,脑子有些浑了
goodname 2009-05-27
  • 打赏
  • 举报
回复
我想了想,觉得
wchar_t elementArray[][][31];
这样定义显然是错误的
所以只能是
wchar_t (* elementArray)[31];
可是这种似乎只能是对付第一维确定的数组。
所以觉得没啥用处。还是不要这样实现的好。
duyouxuanduyouxuan 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 goodname 的回复:]
void func(int *p,int x,int y,int z)
{
for(int i=0;i <x;++i)
{
for(int j=0;j <y;++j)
{
for(int k=0;k <z;++k)
{
cout < < *(p+i*y*z+j*z+k) < < endl;
}
}
}

//int size = x*y*z;

//for(int i=0;i <size;++i)
//{
// cout < < p[i] < < endl;
//}
}


int main()
{
int a[2][2][3] = {
7111,7112,7113,7121,7122,7123,
7211,7212,7213,7221,7222,7223
};

int b[3]…
[/Quote]

呵呵 ,很好的思路,
这个也不错:
int a[2][3][4]={1,3,4};
int ***b=(int***)new int[2];
for (int i=0;i<2;i++)
{
b[i]= (int**)new int[3];
for (int j=0;j<3;j++)
{
b[i][j]=new int[4];
}
}
for (int i=0;i<2;i++)
for (int j=0;j<3;j++)
for (int k=0;k<4;k++)
{
b[i][j][k]=a[i][j][k];
cout<<a[i][j][k]<<endl;
}

goodname 2009-05-27
  • 打赏
  • 举报
回复
void func(int *p,int x,int y,int z)
{
for(int i=0;i<x;++i)
{
for(int j=0;j<y;++j)
{
for(int k=0;k<z;++k)
{
cout << *(p+i*y*z+j*z+k) << endl;
}
}
}

//int size = x*y*z;

//for(int i=0;i<size;++i)
//{
// cout << p[i] << endl;
//}
}


int main()
{
int a[2][2][3] = {
7111,7112,7113,7121,7122,7123,
7211,7212,7213,7221,7222,7223
};

int b[3][2][3] = {
8111,8112,8113,8121,8122,8123,
8211,8212,8213,8221,8222,8223,
8311,8312,8313,8321,8322,8323
};


func((int *)a,2,2,3);

func((int *)b,3,2,3);

}
--------------------

wchar_t elementArray[][][31]这样似乎搞不定哦,或许不行。
duyouxuanduyouxuan 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 goodname 的回复:]
引用 17 楼 goodname 的回复:
引用 16 楼 duyouxuanduyouxuan 的回复:
能让3维数组两维不确定,只确定一维??
wchar_t elementArray[][][31] 能这样定义和使用吗??


不能

不过,如果你能动态的确定这2个维的大小的话,可以采用传递1维指针的方式,然后根据维数来确定位置即可。


说的似乎有些绝对,应该说是可能的。但是得好好的想想如何使用。
[/Quote]

等你好消息。
duyouxuanduyouxuan 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lingyin55 的回复:]
楼主参看这个帖子

http://topic.csdn.net/u/20080730/20/c75e3bdc-7704-44ff-8160-6ffd8e9ddda0.html
[/Quote]

说的很好。3个维度。
goodname 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 goodname 的回复:]
引用 16 楼 duyouxuanduyouxuan 的回复:
能让3维数组两维不确定,只确定一维??
wchar_t elementArray[][][31] 能这样定义和使用吗??


不能

不过,如果你能动态的确定这2个维的大小的话,可以采用传递1维指针的方式,然后根据维数来确定位置即可。
[/Quote]

说的似乎有些绝对,应该说是可能的。但是得好好的想想如何使用。
duyouxuanduyouxuan 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 goodname 的回复:]
引用 16 楼 duyouxuanduyouxuan 的回复:
能让3维数组两维不确定,只确定一维??
wchar_t elementArray[][][31] 能这样定义和使用吗??


不能

不过,如果你能动态的确定这2个维的大小的话,可以采用传递1维指针的方式,然后根据维数来确定位置即可。
[/Quote]

不是很明白,能写个详细点的例子吗?
duyouxuanduyouxuan 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 goodname 的回复:]
引用 12 楼 goodname 的回复:
size也就是第一维的长度,应该由主函数传递过去,在子函数中算不出来的。

elementArray[x][y][z]应该为

elementArray + x*8*31+y+31+z;
-------------------------------------

或者你将函数参数定义为wchar_t elementArray[][8][31]就可以。


只要是将数组传递过去,数组参数就退化为指针了,所以在子函数中无法取得数组的大小,所以大小只能通过参数传递过去。

数组无论…
[/Quote]
很重要。很好。
goodname 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 duyouxuanduyouxuan 的回复:]
能让3维数组两维不确定,只确定一维??
wchar_t elementArray[][][31] 能这样定义和使用吗??
[/Quote]

不能

不过,如果你能动态的确定这2个维的大小的话,可以采用传递1维指针的方式,然后根据维数来确定位置即可。
duyouxuanduyouxuan 2009-05-27
  • 打赏
  • 举报
回复
能让3维数组两维不确定,只确定一维??
wchar_t elementArray[][][31] 能这样定义和使用吗??
ltc_mouse 2009-05-27
  • 打赏
  • 举报
回复
另外,size_t nodeNumber = sizeof(elementArray)/sizeof(elementArray[0]);
这么计算数量是不对的... 数组作为参数,已退化为指针,这里无法求出实际大小...
goodname 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 goodname 的回复:]
size也就是第一维的长度,应该由主函数传递过去,在子函数中算不出来的。

elementArray[x][y][z]应该为

elementArray + x*8*31+y+31+z;
-------------------------------------

或者你将函数参数定义为wchar_t elementArray[][8][31]就可以。
[/Quote]

只要是将数组传递过去,数组参数就退化为指针了,所以在子函数中无法取得数组的大小,所以大小只能通过参数传递过去。

数组无论多少维,都是在内存中连续存放的。而且最右边的那个维数先变化,增加到最大后再变化次右那个,有点类似于进位的概念。
lingyin55 2009-05-27
  • 打赏
  • 举报
回复
goodname 2009-05-27
  • 打赏
  • 举报
回复
size也就是第一维的长度,应该由主函数传递过去,在子函数中算不出来的。

elementArray[x][y][z]应该为

elementArray + x*8*31+y+31+z;
-------------------------------------

或者你将函数参数定义为wchar_t elementArray[][8][31]就可以。
加载更多回复(11)

65,183

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧