【问题标题】:error: invalid types 'int[int]' for array subscript错误:数组下标的类型“int [int]”无效
【发布时间】:2015-09-26 22:44:14
【问题描述】:
#include <iostream>
using namespace std;
int t,n,k,m,i,j;
int l[100002],r[100002],c[100002],a[100002],total=0;
int swap( int *a, int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
int pivot( int l, int h)
{
    int x=c[h],i=l,j=l-1,temp;
    for(i=l;i<h;i++)
    {
        if(c[i]<x)
        {
            j++;
            swap(&c[i],&c[j]);
            swap(&r[i],&r[j]);
            swap(&l[i],&l[j]);
        }
    }
    j++;
    swap(&c[h],&c[j]);
    swap(&l[h],&l[j+1]);
    swap(&r[h],&r[j+1]);
    return j;
}
int quick( int l, int h)
{
    int p;
    if(l<h)
    {
        p=pivot(l,h);
        quick(l,p-1);
        quick(p+1,h);
    }
}
int main() {
    cin>>t;
    while(t--)
    {
        total=0;
        cin>>n>>k>>m;
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            total+=a[i];
        }
        for(i=1;i<=m;i++)
            cin>>l[i]>>r[i]>>c[i];
        quick(1,m);
        for(i=1;i<=m;i++)
            cout<<l[i]<<r[i]<<c[i]<<endl;
    }
    return 0;
}

在上面的代码中,我收到以下错误。

prog.cpp: In function 'int pivot(int, int)':
prog.cpp:21:13: error: invalid types 'int[int]' for array subscript
    swap(&l[i],&l[j]);
             ^
prog.cpp:21:19: error: invalid types 'int[int]' for array subscript
    swap(&l[i],&l[j]);
                   ^
prog.cpp:26:11: error: invalid types 'int[int]' for array subscript
  swap(&l[h],&l[j+1]);
           ^
prog.cpp:26:19: error: invalid types 'int[int]' for array subscript
  swap(&l[h],&l[j+1]);
                   ^

你能弄清楚错误是什么吗?而且我想知道为什么我只在数组l 而不是rc 中收到此错误。所以也请解释一下。

这是上面程序的链接。

http://ideone.com/K1exEK

【问题讨论】:

  • i, 1, l,I, |, j - 你就是这么爱你的读者吗? :-)
  • 这是一个混淆代码的挑战吗?

标签: c++ c arrays parameter-passing quicksort


【解决方案1】:

lint类型的函数参数,它隐藏了同名的全局变量。

【讨论】:

  • 哦,伙计..我犯了一个多么愚蠢的错误......虽然谢谢你指出。
  • @AnujGarg 您与来自namespace std 的名称有名称冲突。从您的代码中删除 using namespace std;。另见this question
  • 那么 using namespace std; 的实际用途是什么
  • @AnujGarg 这远远超出了这个问题的主题。我相信你应该阅读decent C++ book
猜你喜欢
  • 2013-02-12
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 2010-09-26
  • 2012-09-07
相关资源
最近更新 更多