[英]Unused variable error, altought it shouldent
我的C ++程序出现错误。 我需要创建2个排序列表,第3个从前2个排序出来。
void arrayInp()
/* Create 2 vectors by the length defined by the user*/
{
int a,b,c,d,i,j;
Array A,B; /* HERE i get the error used varaibls, why?*/
int rez[20];
cout<<"enter length of the first array: ";
cin>>a;
cout<<"enter length of the second array: ";
cin>>b;
cout<<"insert first array:";
for (i=0;i<=a;i++)
cin>>c;
A.els[i]=c;
cout<<", ";
cout<<"insert second array:";
for (j=0;j<=a;j++)
cin>>d;
B.els[j]=d;
cout<<", ";
}
我导入的标头包含:
const int dim = 10;
struct Array
{
int n;
int els[dim];
};
谢谢您的帮助
该警告可能来自rez
,您不会使用它。
第一次查看代码时,我可以断定您来自python。 该代码导致未定义的行为(可能取决于要成为的索引):
int a,b,c,d,i,j;
Array A,B; /* HERE i get the error used varaibls, why?*/
//...
for (i=0;i<=a;i++)
cin>>c;
A.els[i]=c;
cout<<", ";
看到错误了吗?
for (i=0;i<=a;i++)
{
cin>>c;
}
A.els[i]=c;
cout<<", ";
现在怎么样?
如果您是初学者,请尝试让Clang编译您的代码。 它特别强调可消化的错误消息。
如果您不能使用它,那么您仍然可以使用在线版本 ,尽管它在依赖性方面显然受到限制。
/tmp/webcompile/_1981_1.cc:18:5: warning: unused variable 'rez' [-Wunused-variable]
int rez[20];
^
1 warning generated.
通常,您可以看到用于诊断的信息:
对于此特定警告:实际显示未使用的变量的名称。
帮个忙,得到一个友好的编译器;)
rez
是未使用的变量,不是A
或B
而且您还有其他几个错误。 大括号为一件事。 并且您要求的输入参数超出了用户准备提供的输入参数(在(i=0;i<=a;i++)
)。 然后在第二个块中使用a
代替上边界b
。 复制和粘贴错误?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.