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

products[i,j]与products[i][j]是不是一个意思?为什么不能互换使用?(求知心切,不妥之处,请谅解。谢谢。)

fgfdfg 发布于 2011-07-30 23:18, 544 次点击
Visual C++ 2010,C++/CLI平台

一、正确原程序如下:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
    const int Size(12);
    array<int, 2>^ products(gcnew array<int, 2>(Size, Size));
    Console::WriteLine(L"Here is the {0} times table.", Size);

    for(int i = 0; i <= Size; i++)
        Console::Write(L"______");

    Console::WriteLine();

    Console::Write(L"     |");

    for(int i = 1; i <= Size; i++)
        Console::Write(L"{0,4} |", i);

    Console::WriteLine();

    for(int i = 0; i <= Size; i++)
        Console::Write(L"_____|");

    Console::WriteLine();

    for(int i = 0; i < Size; i++)
    {
        Console::Write(L"{0,4} |", i + 1);

        for(int j = 0; j < Size; j++)
        {
            products[i,j] = (i + 1) * (j + 1);
            Console::Write(L"{0,4} |", products[i,j]);

        }

        Console::WriteLine();
    }

    for(int i = 0; i <= Size; i++)
        Console::Write(L"______");

    Console::WriteLine();
    return 0;
}

但是我将
products[i,j] = (i + 1) * (j + 1);
Console::Write(L"{0,4} |", products[i,j]);
换成
products[i][j] = (i + 1) * (j + 1);
Console::Write(L"{0,4} |", products[i][j]);
就提示错误:
1>test.cpp(37): error C3262: 无效的数组索引: 1 维度为 2-维“cli::array<Type,dimension> ^”指定
1>          with
1>          [
1>              Type=int,
1>              dimension=2
1>          ]
1>test.cpp(37): error C2109: 下标要求数组或指针类型
1>test.cpp(38): error C3262: 无效的数组索引: 1 维度为 2-维“cli::array<Type,dimension> ^”指定
1>          with
1>          [
1>              Type=int,
1>              dimension=2
1>          ]
1>test.cpp(38): error C2109: 下标要求数组或指针类型

这是什么原因?为什么这里只能用products[i,j],而不能用products[i][j]

二、正确原程序如下:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
    array<array<String^>^>^ grades = gcnew array<array<String^>^>
    {
        gcnew array<String^>{"Al", "Blue"},
        gcnew array<String^>{"Lily", "Lucy"},
        gcnew array<String^>{"Line", "Bile"},
        gcnew array<String^>{"King", "Jack"},
        gcnew array<String^>{"Bete", "Tome"},
    };

    wchar_t gradeLetter('A');
    for(int i = 0; i < grades -> Length; i++)
    {
        Console::WriteLine();
        Console::WriteLine(L"Students of the {0} grade:", gradeLetter++);
        for(int j = 0; j < grades[i] -> Length; j++)
            Console::Write(L"{0,12}", grades[i][j]);
    }
    Console::WriteLine();
    return 0;
}

但是我将
Console::Write(L"{0,12}", grades[i][j]);
换成
Console::Write(L"{0,12}", grades[i,j]);
就提示错误:
1>test.cpp(64): error C3262: 无效的数组索引: 2 维度为 1-维“cli::array<Type> ^”指定
1>          with
1>          [
1>              Type=cli::array<System::String ^> ^
1>          ]

这是什么原因?为什么这里只能用grades[i][j],而不能用grades[i,j]

谢谢。
5 回复
#2
lucky5635912011-07-31 06:56
一个是数组,一个是二维坐标
#3
雾都孤儿2011-07-31 11:15
错误判断多明白啊,“error C3262: 无效的数组索引: 2 维度为 1-维“cli::array<Type>”很显然是数组与坐标的关系。
#4
nature03102011-07-31 15:27
products[i,j]中用到了逗号表达式, 逗号表达式的一般形式是exp1,exp2, 它首先计算exp1,然后计算exp2, 整个表达式的结果是exp2的结果. 所以在products[i,j]中, 逗号表达式 i,j首先计算i, 再计算j, 然后返回第2个表达式的结果, 即j. 所以, products[i,j]实际上等于products[j], 这个其实是一个常量指针, 常量指针是不可以赋值的, 所以你的表达式语句products[i,j] = (i + 1) * (j + 1);是非法的.
#5
fgfdfg2011-07-31 22:06
回复 4楼 nature0310
products[i,j] = (i + 1) * (j + 1);
是可以正常运行的。
products[i][j] = (i + 1) * (j + 1);
也是出错的。
#6
fgfdfg2011-07-31 22:08
回复 2楼 lucky563591
不明白,能不能解释详细一些。谢谢。
1