2011-12-28 55 views
0

#include这些标题:ifstream的的<fstream>不会编译

#include <iostream> 
#include <fstream> 

但却这段代码:

ifstream inFile; 

仍然不会编译。可能是什么问题呢?我使用Visual Studio 2010,Win32 C++。

+3

**一般的建议:**,我们希望看到一个测试用例知道你做了什么错;错误信息也不会伤害。下面的答案将是猜测。 – 2011-12-28 02:41:19

回答

4

您可以将using namespace std;置于代码的顶部,因此您不必完全限定标准C++的内容,但它被大量开发人员认为是不好的形式。

我只是前缀,在标准的东西与std::,这使得代码不再:

std::cout << "Hello, world.\n"; 

但让我走出困境面对面的人命名空间冲突。

下面的记录表明在行动中使用的std::前缀:

$ cat testprog.cpp 
    #include <iostream> 
    #include <fstream> 

    int main (void) { 
     int n; 
     std::ifstream inFile("input.txt"); 
     inFile >> n; 
     std::cout << "File contained " << n << '\n'; 
     return 0; 
    } 

$ cat input.txt 
42 

$ g++ -Wall -Wextra -o testprog testprog.cpp ; ./testprog 
File contained 42 
+0

文件包含42 ... :) – 2011-12-28 02:56:18

3

该类型是std::ifstream。除非您通过其他方式将限定名称带入范围,否则必须全部写出。

+0

我完全忘了它。谢谢! – 2011-12-28 02:44:51