【问题标题】:Issues with "'x' was not declared in this scope" [closed]在此范围内未声明“'x'”的问题[关闭]
【发布时间】:2018-11-09 13:22:02
【问题描述】:

我试图编写一个简单的代码来尝试求解矩阵,但遇到了一个错误。显然,我只是在做一些愚蠢的事情,所以我希望你能来救援!每当我运行代码并输入值时,它返回的“x”都没有在这个范围内声明。有什么想法吗?

#include <iostream>
using namespace std;

int main() {

// Row 1
cout << "Please input the first row, first column (R1:C1)" << endl;
int R1C1;
cin >> R1C1;
cout << "Please input the first row, second column (R1:C2)" << endl;
int R1C2;
cin >> R1C2;
cout << "Please input the first row, third column (R1:C3)" << endl;
int R1C3;
cin >> R1C3;
cout << "Please input the first row, fourth column (R1:C4)" << endl;
int R1C4;
cin >> R1C4;

// Row 2
cout << "Please input the second row, first column (R2:C1)" << endl;
int R2C1;
cin >> R2C1;
cout << "Please input the second row, second column (R2:C2)" << endl;
int R2C2;
cin >> R2C2;
cout << "Please input the second row, third column (R2:C3)" << endl;
int R2C3;
cin >> R2C3;
cout << "Please input the second row, fourth column (R2:C4)" << endl;
int R2C4;
cin >> R2C4;

// Row 3
cout << "Please input the third row, first column (R3:C1)" << endl;
int R3C1;
cin >> R3C1;
cout << "Please input the third row, second column (R3:C2)" << endl;
int R3C2;
cin >> R3C2;
cout << "Please input the third row, third column (R3:C3)" << endl;
int R3C3;
cin >> R3C3;
cout << "Please input the third row, fourth column (R2:C4)" << endl;
int R3C4;
cin >> R3C4;

if (R1C1 > 1)
int x = R1C1 * (1/R1C1);
cout << x; 
return 0;
}

【问题讨论】:

    标签: c++ error-handling declare


    【解决方案1】:

    这里

    if (R1C1 > 1)
    int x = R1C1 * (1/R1C1);
    cout << x;
    

    基本上是这个意思:

    if (R1C1 > 1){
        int x = R1C1 * (1/R1C1);
    }
    cout << x;
    

    如您所见,当您打印时,x 已不在范围内。它的范围仅限于if 表达式的主体。为了减少混淆,人们通常会缩进int x... 行,因此很明显它属于if 语句,并且与cout 行不在同一范围内。

    也许你想这样做:

    if (R1C1 > 1){
        int x = R1C1 * (1/R1C1);
        cout << x;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 2021-12-16
      • 2013-10-25
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多