繁体   English   中英

未使用的变量错误,应将其改正

[英]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是未使用的变量,不是AB

而且您还有其他几个错误。 大括号为一件事。 并且您要求的输入参数超出了用户准备提供的输入参数(在(i=0;i<=a;i++) )。 然后在第二个块中使用a代替上边界b 复制和粘贴错误?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM