2014-03-29 112 views
0
#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; 
} 

我想通過輸入文件通過輸入程序,但是,下面的錯誤彈出,錯誤:C++需要所有聲明的類型說明符?

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

回答

3

無法使用功能的功能或任何其他可執行部分之外的程序。

你的程序就相當於

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

好的,謝謝我的壞。順便說一句我已經包括cstdio –

+0

我看到了,不要忘記,預處理器指令只是「文本替換」,所以你需要小心,你把它們放在哪裏 –

+0

是的,謝謝! –