gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:12:16 25 4
gpt4 key购买 nike

#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;
}`

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

我没有声明一维数组而是像二维数组那样使用,所以我收到了这个错误...请帮忙我得到的错误是

invalid types 'int[int]' for array subscript

最佳答案

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

关于c++ - 使用 vector 的数组下标的无效类型 'int[int]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com