注册 登录
编程论坛 C++教室

明明声明了,为什么程序说我没有声明?

GeneralJing 发布于 2011-10-08 10:55, 1243 次点击
1>ClCompile:
1>  multidimensionalarrays.cpp
1>d:\program files\microsoft visual studio 10.0\练习项目文件夹\multidimensionalarrays\multidimensionalarrays\multidimensionalarrays.cpp(30): error C2065: “i”: 未声明的标识符
1>
1>生成失败。
代码如下:
#include <iostream>
using std::cout;
using std::endl;

void printArray( const int [][ 3 ] );

int main()
{
    int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
    int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
    int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };

    cout << "Values in array1 by row are:" << endl;
    printArray( array1 );

    cout << "\nValues in array2 by row are:" << endl;
    printArray( array2 );

    cout << "\nValues in array3 by row are:" << endl;
    printArray( array3 );
    return 0;
}

void printArray( const int a[][ 3 ] )
{
   
    for ( int i= 0; i < 2; i++ );
    {
        for ( int j = 0; j < 3; j++ )
            cout << a[ i ][ j ] << ' ';

        cout << endl;
    }
}
12 回复
#2
naruto012011-10-08 11:17
http://zhidao.baidu.com/question/295267951.html
#3
rjsp2011-10-08 11:22
for ( int i= 0; i < 2; i++ );
这句话后面有个分号,你想干什么呢?
#4
naruto012011-10-08 12:19
http://zhidao.baidu.com/question/295267951.html
#5
lqsh2011-10-08 16:02
程序代码:
#include <iostream>
using std::cout;
using std::endl;

void output(int *a,int n)//输出数组元素
{
   for(int i=0;i<n;i++)
   {
       cout<<*(a+i)<<" ";
   }
    cout<<endl;
}
void printArray(int*a[])//函数原型声明
{
        for(int i=0;i<2;i++)
    {
       output(*(a+i),3);
    }

}


int main()
{
    int i;

    int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
    int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
    int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
    int *a0[2]={array1[0],array1[1]};//转化为指针数组
    int *a1[2]={array2[0],array2[1]};
    int *a2[2]={array3[0],array3[1]};
  

    cout << "Values in array1 by row are:" << endl;
    printArray(a0);
      

    cout << "\nValues in array2 by row are:" << endl;
    printArray(a1);


    cout << "\nValues in array3 by row are:" << endl;
    printArray(a2);
   
    return 0;
}
[local]1[/local]

#6
lucky5635912011-10-08 17:39
自己打错了呗。
#7
黄昏的王座2011-10-08 18:30
你的那个i并不是全局的i,只是在for循环里面才能用,你必须声明全局的变量i。
#8
hoho5682011-10-08 19:25
呵呵,问题前面已经发现了,就是第一个for循环后面的分号;
有了这个符号,i的作用域就结束了。只在for循环括号里面,后面的自然没有声明。去掉就ok了。。
#9
樱花雾海2011-10-08 22:46
就是第一行for结构后面的分号作怪,以后要注意点洛,我也是经常犯这些错误
#10
GeneralJing2011-10-12 11:04
回复 9楼 樱花雾海
谢谢,是我太粗心了哈哈
#11
GeneralJing2011-10-12 11:05
回复 8楼 hoho568
谢谢大家!感激不尽哈哈
#12
GeneralJing2011-10-12 11:06
回复 3楼 rjsp
Thank you!
#13
日的起烟烟2011-10-16 10:29
你明明没有声明,你为什么说你明明声明了。
1