c++ - "' x' 的问题未在此范围内声明”

标签 c++ error-handling declare

<分区>

我试图编写一个简单的代码来尝试求解矩阵,但遇到了一个错误。很明显,我只是在做一些愚蠢的事情,所以我希望你能来救援!每当我运行代码并输入值时,它都会返回 'x' was not declared in this scope。有什么想法吗?

#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;
}

最佳答案

这里

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;
}

关于c++ - "' x' 的问题未在此范围内声明”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53226516/

相关文章:

c++ - 带有命名对象的复制构造函数

actionscript-3 - 如何处理 AS3 中启用 HTML 的文本字段的错误

PHP:register_shutdown_function 并不总是适用于 Symfony 2

mysql - (SQL) 如何根据另一个表的 ID 将值添加到一个表中?

c++ - SFML 2 绘图在与 OGRE 连接时不起作用

c++ - 未设置 XMLLINT 且未在路径中找到 xmllint;跳过 xml 预处理

r - R 中具有复杂函数和 plyr 的 tryCatch

c# - 类型 'x' 的声明中缺少部分修饰符 - 由设计者自动生成的代码引起

c - 声明一个结构数组

c++ - 可以输入单词的计算器