Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 904
rach Avatar asked May 09 '11 02:05

rach


2 Answers

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.

like image 59
RageD Avatar answered Jan 20 '23 11:01

RageD


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");
}
like image 21
jwismar Avatar answered Jan 20 '23 11:01

jwismar