使用vector的数组下标的无效类型'int [int]'

时间:2021-06-25 16:10:22
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int t;
    cin >> t;
    char g;
    vector<int> b(t);
    vector<int> c(t);
    for(int a=0; a<t ; a++)
        {
        cin>>b[a]>>g>>c[t];
        //this g was not required as a space as input is to be given
    }
    for(int a=0; a<t ; a++)
        {
        float z,x;
        int q,w,c,v,ans=0;
        z=sqrt(b[a]); 
        x=sqrt(c[a]);
        //here the line above error is coming`enter code here`
        c=ceil(z);
        v=ceil(x);
        q=z;
        w=x;
        if((c==v)&&(v!=w))
            {
            ans=0;
        }
        else if((c==v)&&(v==w))
            {
            ans=1;
        }
        else
            {
            while(c<=v)
                {
                ans++;
                c++;
            }
        }
        cout<<ans<<endl;
   }
    return 0;
}`

I know this error but i dot understand why it is coming in this code... please help...

我知道这个错误,但我明白为什么它会出现在这段代码中...请帮忙......

I am not declaring 1 dimensional array and using like a 2 dimensional array so that i m getting this error...please help the error I m getting is

我没有声明一维数组并使用像二维数组,所以我得到这个错误...请帮助我得到的错误是

invalid types 'int[int]' for array subscript

1 个解决方案

#1


2  

In your for loop you created a new integer variable c. This will mask out the original vector c.Since the new c does not support operator[] you get the error. I would solve this problem by using more descriptive variable names especially for your vectors and make sure the name does not match the local integer in your for loop.

在for循环中,您创建了一个新的整数变量c。这将掩盖原始向量c。由于新c不支持operator [],您将收到错误。我会通过使用更具描述性的变量名来解决这个问题,特别是对于你的向量,并确保名称与for循环中的局部整数不匹配。

#1


2  

In your for loop you created a new integer variable c. This will mask out the original vector c.Since the new c does not support operator[] you get the error. I would solve this problem by using more descriptive variable names especially for your vectors and make sure the name does not match the local integer in your for loop.

在for循环中,您创建了一个新的整数变量c。这将掩盖原始向量c。由于新c不支持operator [],您将收到错误。我会通过使用更具描述性的变量名来解决这个问题,特别是对于你的向量,并确保名称与for循环中的局部整数不匹配。