| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 544 人关注过本帖
标题:products[i,j]与products[i][j]是不是一个意思?为什么不能互换使用?(求知 ...
只看楼主 加入收藏
fgfdfg
Rank: 2
等 级:论坛游民
帖 子:25
专家分:52
注 册:2011-5-31
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
products[i,j]与products[i][j]是不是一个意思?为什么不能互换使用?(求知心切,不妥之处,请谅解。谢谢。)
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]

谢谢。
搜索更多相关主题的帖子: 2010 products include 
2011-07-30 23:18
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:20 
一个是数组,一个是二维坐标
2011-07-31 06:56
雾都孤儿
Rank: 1
来 自:山东省沾化县泊头镇明家村
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-7-31
收藏
得分:0 
错误判断多明白啊,“error C3262: 无效的数组索引: 2 维度为 1-维“cli::array<Type>”很显然是数组与坐标的关系。

想通过网络学习,增强编程能力。
2011-07-31 11:15
nature0310
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-2-13
收藏
得分:0 
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);是非法的.
2011-07-31 15:27
fgfdfg
Rank: 2
等 级:论坛游民
帖 子:25
专家分:52
注 册:2011-5-31
收藏
得分:0 
回复 4楼 nature0310
products[i,j] = (i + 1) * (j + 1);
是可以正常运行的。
products[i][j] = (i + 1) * (j + 1);
也是出错的。
2011-07-31 22:06
fgfdfg
Rank: 2
等 级:论坛游民
帖 子:25
专家分:52
注 册:2011-5-31
收藏
得分:0 
回复 2楼 lucky563591
不明白,能不能解释详细一些。谢谢。
2011-07-31 22:08
快速回复:products[i,j]与products[i][j]是不是一个意思?为什么不能互换使用? ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021159 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved