← 返回题目列表

AtCoder · APG4b_am

AP3. 附录3.编译错误集合

中文机器翻译题面 查看原题

返回 EX2「1.02.程序的写法与错误」

编译错误集

在编译时,如果代码有误,会发生编译错误。 本页将说明代表性的编译错误以及对应的处理方法。

本页写入的内容旨在在 AtCoder 上作为“C++14 (GCC 5.4.1)”执行。如果您使用的是其他环境,请在您的环境中运行示例程序并检查错误消息。

如何使用此页面

如果您使用浏览器的搜索功能在页面内搜索错误消息,您可以轻松找到具有相应错误的文章。

对于 Chrome

  • 单击工具栏右上角的 Chrome 菜单。
  • 单击搜索 (F)。
  • 在右上角显示的搜索栏中输入您要搜索的关键字。

对于火狐浏览器 单击

  • 菜单按钮。
  • 单击搜索此页...
  • 在底部显示的搜索字段中输入要搜索的关键字。

对于边缘

  • 按 Ctrl+F。
  • 在屏幕顶部显示的搜索字段中输入要搜索的关键字。

検索方法

  • 复制编译错误
  • 使用浏览器的搜索功能进行搜索。 如果找不到它,请尝试缩短错误消息。

忘记分号

错误消息示例

error: expected ‘,’ or ‘;’ before ‘○○○’
error: expected ';' at end of declaration

原因

您忘记在行尾写入分号或冒号。

导致错误的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a = 2;
  int b = 3
  int c = a + b;
  cout << c << endl;
}

第 6 行 您忘记了行尾的分号。

修正方法

添加分号。 您忘记分号的地方通常靠近错误消息出现的行。

修改后的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a = 2;
  int b = 3;
  int c = a + b;
  cout << c << endl;
}

在第 6 行末尾添加了一个分号。


使用不存在的变量

错误消息示例

error: ‘○○○’ was not declared in this scope

原因

尝试在未声明的情况下使用变量。 您正在使用名为

导致错误的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a = 2;
  int b = 3;
  cout << c << endl;
}

7行目 c 的变量,但未声明它。

修正方法

让我们声明一个变量。

修改后的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a = 2;
  int b = 3;
  int c = a + b;
  cout << c << endl;
}

在第 7 行,我们声明了一个名为 c 的变量。


包含文件名不正确

错误消息示例

fatal error: ○○○.h: No such file or directory

原因

要包含的文件名不正确。

导致错误的代码

#include <bits/stdoooc++.h>
using namespace std;

int main() {
  cout << "Hello World" << endl;
}

修正方法

更正要包含的文件的名称。

修改后的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  cout << "Hello World" << endl;
}

忘记包括

错误消息示例

error: '○○○’ was not declared in this scope

原因

你忘了写include。 不写

导致错误的代码

using namespace std;

int main() {
  cout << "Hello World" << endl;
}

#include<bits/stdc++.h> 不写 cout呀 我正在使用endl。

修正方法

包含您要使用的功能。

修改后的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  cout << "Hello World" << endl;
}

在第一行 我添加了 #include <bits/stdc++.h> 。


函数名和变量名重复

错误消息示例

error: '○○○’ cannot be used as a function

原因

您正在使用类似于函数的变量。 这可能是由于函数名和变量名之间的冲突而发生的。

导致错误的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  vector<int> vec;
  vec = { 25, 100, 64 };

  int max = vec.at(0);
  for(int i = 1; i < vec.size(); i++) {
    max = max(vec.at(i), max);
  }
  cout << max << endl;
}

第10行是返回最大值的函数max存储最大值的函数和变量max有一个变量, 存在名称冲突。

修正方法

更改变量名称以避免名称冲突。 更改为

修改后的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  vector<int> vec;
  vec = { 25, 100, 64 };

  int num_max = vec.at(0);
  for(int i = 1; i < vec.size(); i++) {
    num_max = max(vec.at(i), num_max);
  }
  cout << num_max << endl;
}

変数名 max 的 num_max 。

for 语句中的语法错误

错误消息示例

./Main.cpp: In function ‘int main()’:
./Main.cpp:5:29: error: expected ‘)’ before ‘;’ token
   for(int i = 0; i < 10; i++;) {
                             ^
./Main.cpp:5:30: error: expected primary-expression before ‘)’ token
   for(int i = 0; i < 10; i++;) {

其特征是 for 语句的语法附近有错误。

原因

for 语句中的语法错误

导致错误的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  for(int i = 0; i < 10; i++;) {
    cout << i << endl;
  }
}

第 5 行 for 语句的语法有错误。 增量过程的末尾有一个额外的分号。

修正方法

让我们更正 for 语句的语法,使其正确。

修改后的代码

#include <bits/stdc++.h>
using namespace std;

int main() {
  for(int i = 0; i < 10; i++) {
    std::cout << i << std::endl;
  }
}

删除了不必要的分号。