c++ ifstream undeclared identifier
I am in Visual Studio and am getting 'ifstream undeclared identifier' with this code (same for ofstream)
#include开发者_如何学编程 <iostream>
#include <iomanip>
#include <fstream>
void main()
{
ifstream infile("file.txt");
ofstream outfile("out.txt");
}
do I need to include something else?
You need to scope it. Use using namespace std;
or preface ifstream
and ostream
with std::
For example, std::ifstream
Currently, the compiler does not know where these structures are defined (since they are declared/defined within the std
namespace). This is why you need to scope your structures/functions in this case.
You need to reference the standard namespace (std). Try this:
#include <iostream>
#include <iomanip>
#include <fstream>
void main()
{
std::ifstream infile("file.txt");
std::ofstream outfile("out.txt");
}
You can use
using namespace std;
instead of prefixing everyline with std::
精彩评论