c++ - 未使用的变量错误,虽然它不应该

标签 c++

我的 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<<", ";
}

我导入的 header 包含:

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<<", ";

现在怎么样?

关于c++ - 未使用的变量错误,虽然它不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9720103/

相关文章:

c++ - 在Qt/C++中,如何在没有HOSTS文件的Windows上将x.com域重定向到y.com?

python - 如何将numpy二维数组作为一种可以用C++读取的二进制格式存储到磁盘上

C++流、函数声明等问题

c++ - c++ 中用于 multimap 的 getter 和 setter

c++ - 如何与 Sailfish Silica/QML 中 slider 的值进行交互?

c++ - 如何打印出双向循环链表?

java - 为什么在 C++/Java 和类似语言中以美元符号开头的变量名不好?

c++ cout with float 产生奇怪的结果

c++ - 在 clang RefactoringTool 中替换返回空 map

c++ - 编译后的二进制文件是 'munched'是什么意思?