c++ - 错误 : C++ requires a type specifier for all declarations?

标签 c++ stdin

#include <cstdio>
#include <iostream>
#include <fstream>
#define INPUT_FILE

#ifdef INPUT_FILE
    freopen("test.txt", "r", stdin);
#endif

using namespace std;

int main(int argc, char const *argv[])
{
    int n;
    while(scanf("%d", &n))
        printf("%d\n", n);
    return 0;
}

我试图通过输入文件将输入传递给程序,但是弹出以下错误,

error: C++ requires a type specifier for all declarations
freopen("test.txt", "r", stdin);
    ^~~~~~~
1 error generated.

最佳答案

您不能在函数或程序的任何其他可执行部分之外使用函数。

你的程序等同于

#include <cstdio>
#include <iostream>
#include <fstream>
#define INPUT_FILE


freopen("test.txt", "r", stdin); // Makes no sense

using namespace std;

int main(int argc, char const *argv[])
{
    int n;
    while(scanf("%d", &n))
        printf("%d\n", n);
    return 0;
}

关于c++ - 错误 : C++ requires a type specifier for all declarations?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22729443/

相关文章:

c++ - 如何有效地使用boost mpi广播功能?

c++ - Visual Studio 编译器错误

c - 将字符串传递给 scanf

c - 使用 fflush(标准输入)

c - 从 stdin 读取的程序比从文件读取的效率如何?

c - 如果指向标准输入,是什么让 fgets() 实际上等待用户输入?

c++ - 为什么序列运算算法的谓词是通过拷贝传递的?

c++ - 如何在 Linux 上的 c 中休眠或暂停 PThread

c++ - 如何在 QComboBox 上设置不可选择的默认文本?

javascript - Node process.stdin 需要多次按键才能以原始模式发出数据