【问题标题】:c++ program multiple declaraton errorc++程序多重声明错误
【发布时间】:2018-08-19 12:42:50
【问题描述】:

我在这个 c++ 程序中遇到多个声明错误

#include<iostream.h>
#include<conio.h>
void main ()
{       clrscr();
    int a[10][10],r,q,i;
    cout<<"enter how many rows and colomn you want in the matrix:";
    cin>>n;
    cout<<"enter the matrix \n";
    for(int r=0;r<n;++r)
    {
        for(int q=0;q<n;++q)
        {
            cin>>a[r][q];
        }

    }
        for(int i=0;i<n;i++)
    {   cout<<"\n the diagnol elements are:";
        cout<<a[n-i-1][i];
    }
    getch();
}

这是一个在矩阵中查找诊断元素的程序

【问题讨论】:

标签: syntax-error turbo-c++


【解决方案1】:

那是因为您已经在第 5 行将rqi 声明为int,如下所示:

int a[10][10],r,q,i;
              ^^^^^

当你的三个 for 循环时,再次重新声明例如像这样:

for(int r=0;r<n;++r)
    ^^^

所以它在上述情况下重新声明了相同的变量r,而另一个for循环qi是不允许的。

解决问题的两种方法:

一个。您可以从 for 循环中删除 int。
湾。要么从第 5 行删除 for 循环中使用的变量声明。

【讨论】:

  • 我相信在 for 循环中声明变量不会导致该错误。 wandbox example
【解决方案2】:

我看到的唯一问题是,您没有声明变量 "n" 。除此之外,一切似乎都很好。

【讨论】:

    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 2020-03-20
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    相关资源
    最近更新 更多